views:

261

answers:

1

Is it possible in ASP.NET to dynamically load up a WebControl from a string with some tag contents in it (without writing a bunch of custom code)?

For instance, I have a string like the following:

string controlTag = "<asp:Label ID=\"lblLabel\" runat=\"server\" />";

I then want to do something like the following to load up the control from that string:

WebControl webControl = LoadControlFromTagString(controlTag);

I can simply parse the string myself and dynamically load up a control in LoadControlFromTagString, but I was wondering if there is anything built in to .NET I can take advantage of. Any suggestions?

+5  A: 

There are several choices, depending on what you want to do your control instance (and how much control you want over stuff like rendering, databinding, etcetera).

The easiest is probably TemplateControl.ParseControl(String) which you have access to via your current Page instance.

Simon Svensson