<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Erwin's Blog : HTML</title><link>http://weblogs.asp.net/erwingriekspoor/archive/tags/HTML/default.aspx</link><description>Tags: HTML</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Parse a html string with ASP.NET controls</title><link>http://weblogs.asp.net/erwingriekspoor/archive/2008/11/06/parse-a-html-string-with-asp-net-controls.aspx</link><pubDate>Thu, 06 Nov 2008 20:51:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6724465</guid><dc:creator>erwin21</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/erwingriekspoor/rsscomments.aspx?PostID=6724465</wfw:commentRss><comments>http://weblogs.asp.net/erwingriekspoor/archive/2008/11/06/parse-a-html-string-with-asp-net-controls.aspx#comments</comments><description>
&lt;p&gt;&lt;br&gt;Today I searched for a solution to parse a html string with ASP.NET controls on runtime. A string like for example this: &lt;br&gt;&lt;/p&gt;

&lt;div class="xml" style="border: 1px solid rgb(208, 208, 208); font-family: monospace; color: rgb(0, 0, 102); background-color: rgb(240, 240, 240);"&gt;
&lt;ol&gt;
&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;table&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;tr&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;td&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Name&lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;/td&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;/tr&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;tr&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;td&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;asp:TextBox&lt;/span&gt; &lt;span style="color: rgb(0, 0, 102);"&gt;id&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;"name"&lt;/span&gt; &lt;span style="color: rgb(0, 0, 102);"&gt;runat&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;"server"&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;/td&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp; &amp;nbsp; &lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;/tr&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;lt;/table&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;br&gt;Now I want to parse this string a runtime into a Control object, this can be done with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.parsecontrol.aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.parsecontrol.aspx"&gt;&lt;span class="nu"&gt;&lt;/span&gt;ParseControl&lt;/a&gt; method from the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.aspx"&gt;TemplateControl &lt;/a&gt;class, the ParseControl methods can be accessed from the Page instance, like this for example:&lt;br&gt;&lt;br&gt;

&lt;div class="csharp" style="border: 1px solid rgb(208, 208, 208); font-family: monospace; color: rgb(0, 0, 102); background-color: rgb(240, 240, 240);"&gt;
&lt;ol&gt;
&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;string&lt;/span&gt; html &lt;span style="color: rgb(0, 128, 0);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102);"&gt;"&amp;lt;table&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Name&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;asp:TextBox id=&lt;span style="color: rgb(0, 128, 128); font-weight: bold;"&gt;\"&lt;/span&gt;name&lt;span style="color: rgb(0, 128, 128); font-weight: bold;"&gt;\"&lt;/span&gt; runat=&lt;span style="color: rgb(0, 128, 128); font-weight: bold;"&gt;\"&lt;/span&gt;server&lt;span style="color: rgb(0, 128, 128); font-weight: bold;"&gt;\"&lt;/span&gt; /&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;b&amp;lt;/table&amp;gt;"&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;Control ctrl &lt;span style="color: rgb(0, 128, 0);"&gt;=&lt;/span&gt; Page.&lt;span style="color: rgb(0, 0, 255);"&gt;ParseControl&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;html&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;&amp;nbsp;&lt;/div&gt;&lt;/li&gt;

&lt;li style="vertical-align: top; font-family: 'Courier New',Courier,monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 130%; line-height: normal; font-size-adjust: none; font-stretch: normal; color: rgb(0, 48, 48);"&gt;&lt;div style="margin: 0pt; padding: 0pt; background: transparent none repeat scroll 0% 0%; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; vertical-align: top; color: rgb(0, 0, 32);"&gt;somePlaceHolder.&lt;span style="color: rgb(0, 0, 255);"&gt;Controls&lt;/span&gt;.&lt;span style="color: rgb(0, 0, 255);"&gt;Add&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;ctrl&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;br&gt;
After the html has been parsed the control can be added to for example a placeholder.&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6724465" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/erwingriekspoor/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/erwingriekspoor/archive/tags/HTML/default.aspx">HTML</category></item></channel></rss>