Question

I have a webpage in ASP.NET 3.5 that will be creating WebControls dynamically. The WebControls that it will be creating will be known by their fully qualified path (ie - System.Web.UI.WebControls.whatever). The reason for this is because I am allowing the user to decide what controls will go on the webpage. Of course, there's more complexity than this, but that is it in a nutshell.

Simply put - how do I create a WebControl on a webpage by it's fully qualified path?

I realize that the answer will probably end up using reflection, but I have little experience using reflection and I don't want to shoot myself in the foot by making a newbie mistake.

Was it helpful?

Solution

try to call this way: Activator.CreateInstance(Type.GetType("TypeName"));

where TypeName is fully qualified name, including assembly. in my case it looked this way:

Activator.CreateInstance(Type.GetType("System.Web.UI.WebControls.Label, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"));

to be sure about full name in your case, try to output typeof(System.Web.UI.WebControls.Label).FullName and use it as a pattern

OTHER TIPS

object widget = Activator.CreateInstance ( Assembly.GetType ( name ) );

where name is the string of the fully qualified type

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top