Accessing methods and properties of dynamically loaded controls

J

Jeff Smith

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.
 
M

Martin Dechev

Hi,

Create an interface and implement it in the classes in question. Interfaces
represent a contract, in that a class that implements an interface must
implement every aspect of that interface exactly as it is defined. Then when
you instantiate the controls just cast the instance to this interface and
call the method you need. Just note that if any of these classes does not
implement the interface you will get an invalid cast exception at runtime.
Here's an example:

public interface IInitFromXml
{
void InitControl(XmlDocument data);
}

public class TestControl : UserControl, IInitFromXml
{
public void IInitFromXml.InitControl(XmlDocument data)
{
// actual implementation
}
//....
}

public class NewTestControl : UserControl, IInitFromXml
{
public void IInitFromXml.InitControl(XmlDocument data)
{
// actual implementation
}
//....
}

And in the page:

void CreateControl(string pControl)
{
System.Web.UI.Control oControl;
oControl = LoadControl(pControl + ".ascx");
((IInitFromXml)oControl).InitControl(data);
Controls.Add(oControl);
}

Hope this helps
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top