Parse a html string with ASP.NET controls
Today I searched for a solution to parse a html string with ASP.NET controls on runtime. A string like for example this:
- <table>
- <tr>
- <td>Name</td>
- </tr>
- <tr>
- <td><asp:TextBox id="name" runat="server" /></td>
- </tr>
- </table>
Now I want to parse this string a runtime into a Control object, this can be done with the ParseControl method from the TemplateControl class, the ParseControl methods can be accessed from the Page instance, like this for example:
- 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.