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:

  1. <table>
  2.     <tr>
  3.         <td>Name</td>
  4.     </tr>
  5.     <tr>
  6.         <td><asp:TextBox id="name" runat="server" /></td>
  7.     </tr>
  8. </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:

  1. string html = "<table><tr><td>Name</td></tr><tr><td><asp:TextBox id=\"name\" runat=\"server\" /></td></tr>b</table>"
  3. Control ctrl = Page.ParseControl(html);
  5. somePlaceHolder.Controls.Add(ctrl);

After the html has been parsed the control can be added to for example a placeholder.


Comments have been disabled for this content.