Erwin's Blog

Developing with .NET

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>"
  2.  
  3. Control ctrl = Page.ParseControl(html);
  4.  
  5. somePlaceHolder.Controls.Add(ctrl);

After the html has been parsed the control can be added to for example a placeholder.
Posted: Nov 06 2008, 08:51 PM by erwin21 | with 5 comment(s)
Filed under: ,

Comments

gOODiDEA.NET said:

.NET What's New in the BCL in .NET 4.0 NetMon API &#8211; Capture, Parse and and Capture File Access

# November 6, 2008 7:46 PM

gOODiDEA said:

.NETWhat'sNewintheBCLin.NET4.0NetMonAPI–Capture,ParseandandCaptureFileAccess(wi...

# November 6, 2008 7:49 PM

asdf said:

string html = "<table><tr><td>Name</td></tr><tr><td><asp:TextBox id=\"name\" runat=\"server\" Text='<%#Eval(\"Subject\")%>'/></td></tr>b</table>"

     Control ctrl = Page.ParseControl(html);

somePlaceHolder.Controls.Add(ctrl);

if set " Text='<%#Eval(\"Subject\")%>' "

is error

# November 10, 2008 9:44 PM

james_smith said:

That's a quite useful function..Thanks for sharing!!

# November 17, 2008 8:12 AM

cheap propecia said:

Excellent site, It was pleasant to me.

# May 31, 2009 3:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)