100's of UserControls to dynamically load ... How?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there are equivalent activity in .NET Windows to
MyControl =LoadControl("NamedContol.ascx") ?

I need “loadâ€, add to a ControlsCollection a particular UserControl based on a run-time XML stream. For example by picking off attribute: UseVisualControl=â€SomeUserControlâ€

There will be 100’s of such UserControls available for the stream to request.


From this static, design-time snippet:

Dim MyUserContol As SomeUserControl = New SomeUserControl
Me.Panel1.Controls.Add(MyUserContol)


How can I do this dynamically, creating the requested â€SomeUserControl†on the fly WITHOUT a gigantic case statement:

Select Case UseVisualControl
Case “AUserControlâ€
Dim MyUserContol As AUserControl = New AUserControl
Me.Panel1.Controls.Add(MyUserContol)

Case “BUserControl†…
Dim MyUserContol As BUserControl = New BUserControl
Me.Panel1.Controls.Add(MyUserContol)

Case “CUserControl …
Dim MyUserContol As CUserControl = New CUserControl
Me.Panel1.Controls.Add(MyUserContol)

Beyond this … I would like to be able to catalog the UserControls available as part of the XML editing / creation. Speaking to the user … For this node, here is a list of UserControls you can select from (to set UseVisualControl=â€SomeUserControlâ€). Is there a way to catalog them programmatically?

Regards,
John
 
JohnM said:
Is there are equivalent activity in .NET Windows to
MyControl =LoadControl("NamedContol.ascx") ?

I need “loadâ€, add to a ControlsCollection a particular UserControl based on a run-time XML stream. For example by picking off attribute: UseVisualControl=â€SomeUserControlâ€

There will be 100’s of such UserControls available for the stream to request.


From this static, design-time snippet:

Dim MyUserContol As SomeUserControl = New SomeUserControl
Me.Panel1.Controls.Add(MyUserContol)


How can I do this dynamically, creating the requested â€SomeUserControl†on the fly WITHOUT a gigantic case statement:

Select Case UseVisualControl
Case “AUserControlâ€
Dim MyUserContol As AUserControl = New AUserControl
Me.Panel1.Controls.Add(MyUserContol)

Case “BUserControl†…
Dim MyUserContol As BUserControl = New BUserControl
Me.Panel1.Controls.Add(MyUserContol)

Case “CUserControl …
Dim MyUserContol As CUserControl = New CUserControl
Me.Panel1.Controls.Add(MyUserContol)

Beyond this … I would like to be able to catalog the UserControls available as part of the XML editing / creation. Speaking to the user … For this node, here is a list of UserControls you can select from (to set UseVisualControl=â€SomeUserControlâ€). Is there a way to catalog them programmatically?

Regards,
John

You might try using reflection technology:

see System.Reflection, in addition to System.Type.GetType(sType)

to create an instance of the object use the Activator object:
object o = System.Activator.CreateInstance(Type)

though never tried, i assume the above can be casted to the appropriate
type: myClass a = (myClass)o;

etan bukiet
 
Indeed ! Thank You ... this is just what I need and work well.

Any ideas about the catalog ?
 
Back
Top