473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2400
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonathan Williams | last post by:
Hi, I have an object which inherits from WebControl (CUSTOM : WebControl) In this object I have code in which I add child contols: protected override void CreateChildControls() {...
6
by: Earl Teigrob | last post by:
I am writing an application that dynamically loads user controls at run time based on user options. I would like to give my users the ability to build their own user controls and add them to my...
2
by: Rubble | last post by:
Hello, Ive searched all over the net trying to find an answer to this...so anybody with some expertise in this area would be greatly appreciated. Background: I have a webform that loads a...
2
by: Vivek Sharma | last post by:
Hi There, I have a situation where I wish to load the controls dynamically on the basis of user role. Hence, I am using this code. if (UserRole == "IS Administrator") { Control UC1 =...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
7
by: =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post by:
I have a problem with accessing controls that I have loaded dynamically and added to a web page. The scenario: I have a webpage that displays multiple instances of a user control on the page. ...
9
by: J055 | last post by:
Hi I have a standard asp page which uses a MasterPage. The MasterPage contains a User control. How can I access a public method in the User control from my WebForm page? I can't move the method...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
0
by: porksmash | last post by:
I'm developing an app here that uses ActiveX controls to connect to industrial cameras over Ethernet. I want to be able to dynamically create those controls at runtime based on how many cameras are...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.