469,319 Members | 2,347 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

Accessing methods and properties of dynamically loaded controls

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.
Nov 18 '05 #1
1 2263
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
"Jeff Smith" <ti*********@icm-computer.co.uk> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jonathan Williams | last post: by
6 posts views Thread by Earl Teigrob | last post: by
2 posts views Thread by Rubble | last post: by
2 posts views Thread by Vivek Sharma | last post: by
7 posts views Thread by =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post: by
9 posts views Thread by J055 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.