Reputation: 101
I want to render some content into div section which is in user control( Example.ascx) through code behind file(Example.ascx.cs),
Example.ascx
<div id="DivElement" runat="server">
</div>
Example.ascx.cs
void OnInit(EventArgs e)
{
this.PreRender += new EventHandler(Ex_PreRender);
base.OnInit(e);
}
void Ex_PreRender(object sender, EventArgs e)
{
this.DivElement.Load(@"<input type='hidden' name='{0}' id='{0}_VIEW' value='{2}' />
<input type='hidden' name='{0}' value='{3}' />
<script type='text/javascript'>
$(document).ready(function() {{
_controllers['{0}'] = new ExController({{
id: '{0}',
src: '{1}',
}});
_controllers['{0}'].Load();
}});
</script>",
this.id,
this.src,
Request[this.id + "_VIEWSTATE"];);
}
I tried like above but the content not load into the DivElement. Can anyone suggest how to render the above elements into div?
Upvotes: 0
Views: 100
Reputation: 3720
I would probably use a literal.
<div><asp:literal id="litContent" runat="server"></div>
Then in your code-behind, use:
protected void Page_Load(object sender, EventArgs e)
{
litContent.Text = "Your Content";
}
I also notice you have a few errors in your string. If you want to insert strings into strings, you will need to use string.Format().
Here's an example:
litContent.Text = string.Format("hello {0}. I am a {1}", "you", "test");
So, looking at your string, you will want to do something like this:
protected void Page_Load(object sender, EventArgs e)
{
string id = "fetchThisFromSomewhere";
string src = "fetchThisToo";
litContent.Text = string.Format(@"
<input type='hidden' name='{0}' id='{0}_VIEW' value='{2}' />
<input type='hidden' name='{0}' value='{3}' />
<script type='text/javascript'>
$(document).ready(function() {{
_controllers['{0}'] = new ExController({{
id: '{0}',
src: '{1}',
}});
_controllers['{0}'].Load();
}});
</script>",
id,
src,
Request[id + "_VIEWSTATE"]);
}
Upvotes: 1