Can I load custom web user controls dynamically and access the properties
and methods without having to explicitly define custom control types
(example 2 below). I have custom web control named EditStuff.ascx which
reads from an xml file and loads controls to its self based on string value
in xml nodes collection of the xml.
There are several controls that can be loaded and for each one there exists
a public method called 'IntiControl' which is a void method whose signature
accepts an xml file as a parameter. Is it possible to load the customer web
controls into my main control and have access to the properties and methods
of the control without explicitly having to invoke method for the particular
type of web control as in example 2 below.
1. The example below assumes 'TestControl' is the name of the control
which is passed into the create control method, obvously control not known
until runtime so InitControl method not known about and will not compile if
attempt to code the InitControl method this way.
string sControl = "TestControl";
CreateControl(sControl);
public string CreateControl(string pControl){
System.Web.UI.Control oControl;
oControl = LoadControl(pControl + ".ascx");
// oControl.InitControl(data); //will not work as methods
and properties unknown till runtime
Controls.Add(oControl);
}
2. The explicit naming approach comprises of this method below which is
inflexible as new case statement required for each new control added to the
..net solution.
public string CreateControl(string pControl){
System.Xml.XMLDocument data = new XMLDocument();
data.load("c:\\TestControl.xml");
switch (pControl){
case "TestControl":
TestControl oTest =
(TestControl)LoadControl(pControl + " .ascx");
oTest.InitControl(data);
Controls.Add(oTest);
break;
case "TestControlNew":
TestControlNew oTestNew = (
TestControlNew)LoadControl(pControl + " .ascx");
oTestNew.InitControl(data);
Controls.Add(oTestNew);
break;
}
}
Thanks.