0

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?

  • Why are you dynamically creating controls based on a string? Dynamically loading controls can be messy business and if you aren't familiar with viewstate you will probably run into problems. – angelo Jun 24 '09 at 15:08

1 Answers1

6

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.

sisve
  • 19,501
  • 3
  • 53
  • 95