If you want to parse a html string at runtime with asp.net control in it you can use the method ParseControl, for example if you have a html fragment like this:
<table>
<tr>
<td>Name</td>
</tr>
<tr>
<td><asp:TextBox id="name" runat="server" /></td>
</tr>
</table>
Now if you want to parse this string at runtime into a Control object, you can do this with the ParseControl method from the TemplateControl class, the ParseControl methods can be accessed from the Page instance, like this:
string html = "<table><tr><td>Name</td></tr><tr><td><asp:TextBox id=\"name\" runat=\"server\" /></td></tr>b</table>"
Control ctrl = Page.ParseControl(html);
somePlaceHolder.Controls.Add(ctrl);
After the html has been parsed the control can be added to for example a placeholder.
Revision number 2, Thursday, December 04, 2008 4:34:31 PM by syed.tayyab.ali
You must Login to comment.
|
Thu, Nov 27, 2008 12:44 AM
by dillibabu
|
Seems to be simple but very helpfull.
|
|
Sat, Dec 6, 2008 2:09 PM
by LeBear
|
I was just recently trying to figure out how to allow the addition of a server-side control in content generated in a CMS. Discussions were that this wasn't possible, which made sense. After careful consideration of the security issues, I think I'll be able to use this. Thanks.
|
|
Tue, Dec 9, 2008 11:53 AM
by rami_nassar
|
simple and helpful
|
|
Wed, Dec 10, 2008 2:09 PM
by goblyn27
|
I could be wrong (its been a while so I am not positive j/k) but I believe that is will be important that you parse the string and add the resulting control to the page's control tree BEFORE the page lifecycle's LoadViewState event on postback.
|
|
Mon, Dec 15, 2008 11:34 PM
by vik20000in
|
pretty cool
|
|
Fri, Dec 26, 2008 8:43 AM
by sirdneo
|
Smart work like this can saves lots of time
|
|
Thu, Apr 23, 2009 3:33 AM
by sunilrajkg
|
Hi,
I want to use a linkbutton. I put the code inside the html. - asp:LinkButton id=\"lbReply\" runat=\"server\" OnClick=\"lbReply_Click\">Reply/asp:LinkButton>".
This creates a linkbutton control. But the postback doesnt call the event lbReply_Click(). Any help on this regard would be greatly appreciated.
|
|
Wed, Mar 31, 2010 7:23 AM
by shahid13
|
hi sunil try this
protected void GenerateControl()
{ LinkButton lb = new LinkButton(); lb.ID = "lb"; lb.Click += new EventHandler(lb_Click); lb.Text = "click me"; pnlPlace.Controls.Add(lb);
} void lb_Click(object sender, EventArgs e) { Response.Write("hello"); }
|