473,725 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = "TestContro l";
CreateControl(s Control);

public string CreateControl(s tring pControl){
System.Web.UI.C ontrol oControl;
oControl = LoadControl(pCo ntrol + ".ascx");
// oControl.InitCo ntrol(data); //will not work as methods
and properties unknown till runtime
Controls.Add(oC ontrol);
}

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(s tring pControl){

System.Xml.XMLD ocument data = new XMLDocument();
data.load("c:\\ TestControl.xml ");

switch (pControl){

case "TestContro l":
TestControl oTest =
(TestControl)Lo adControl(pCont rol + " .ascx");
oTest.InitContr ol(data);
Controls.Add(oT est);
break;

case "TestControlNew ":
TestControlNew oTestNew = (
TestControlNew) LoadControl(pCo ntrol + " .ascx");
oTestNew.InitCo ntrol(data);
Controls.Add(oT estNew);
break;
}
}

Thanks.
Nov 18 '05 #1
1 2417
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(Xml Document data);
}

public class TestControl : UserControl, IInitFromXml
{
public void IInitFromXml.In itControl(XmlDo cument data)
{
// actual implementation
}
//....
}

public class NewTestControl : UserControl, IInitFromXml
{
public void IInitFromXml.In itControl(XmlDo cument data)
{
// actual implementation
}
//....
}

And in the page:

void CreateControl(s tring pControl)
{
System.Web.UI.C ontrol oControl;
oControl = LoadControl(pCo ntrol + ".ascx");
((IInitFromXml) oControl).InitC ontrol(data);
Controls.Add(oC ontrol);
}

Hope this helps
Martin
"Jeff Smith" <ti*********@ic m-computer.co.uk> wrote in message
news:uJ******** ******@TK2MSFTN GP11.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 = "TestContro l";
CreateControl(s Control);

public string CreateControl(s tring pControl){
System.Web.UI.C ontrol oControl;
oControl = LoadControl(pCo ntrol + ".ascx");
// oControl.InitCo ntrol(data); //will not work as methods
and properties unknown till runtime
Controls.Add(oC ontrol);
}

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(s tring pControl){

System.Xml.XMLD ocument data = new XMLDocument();
data.load("c:\\ TestControl.xml ");

switch (pControl){

case "TestContro l":
TestControl oTest =
(TestControl)Lo adControl(pCont rol + " .ascx");
oTest.InitContr ol(data);
Controls.Add(oT est);
break;

case "TestControlNew ":
TestControlNew oTestNew = (
TestControlNew) LoadControl(pCo ntrol + " .ascx");
oTestNew.InitCo ntrol(data);
Controls.Add(oT estNew);
break;
}
}

Thanks.

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1708
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() { //this.Controls.Clear(); CustomFieldUtil objCustomFieldUtil = new CustomFieldUtil(); DataTable objDT = objCustomFieldUtil.GetDataTable("custom"); for(intCount = 0; intCount < objDT.Rows.Count; intCount++)
6
2571
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 user control folder so that they can be selected and loaded at run time also. These new controls will need to have access to many of the classes with there properties and methods within the complied code. Is this possible? This seems to be the...
2
1344
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 datagrid. This datagrid has a button in a column that loads another datagrid(user control) when clicked. This new grid also has a column with a button that loads another user control. So, there could be 3 user controls on this form if all the
2
1711
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 = LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");
6
11076
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 how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
7
2132
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. The number of controls that are displayed can be adjusted by the user so the controls are added to the page dynamically using the following code in the Page_Init event handler: For i as Integer = 1 to NumberOfControls
9
2651
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 to another location because it populates a Textbox in the user control page. Thanks Andrew
2
3117
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 dynamically loaded assembly) so far so good (per my code below)... but here is where I'm getting hung up: 3. Call methods of that type (see comments in my code) If the types in the dynamically loaded assembly were in the same namespace
0
1432
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 configured, rather than take the easy way out and put a fixed amount of controls on the form at design. Makes it more flexible. At the moment the way I am trying to do it is adding instances of the control into a collection with their individual...
0
8878
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8749
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9398
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9097
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4776
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2626
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2153
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.