Accessing methods and properties of dynamically loaded controls

Discussion in 'ASP .Net' started by Jeff Smith, Oct 11, 2004.

  1. Jeff Smith

    Jeff Smith Guest

    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.
    Jeff Smith, Oct 11, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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.
    >
    >
    Martin Dechev, Oct 11, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QnJpYW4=?=
    Replies:
    1
    Views:
    478
    Brock Allen
    May 6, 2005
  2. Vivek Sharma
    Replies:
    2
    Views:
    718
    Vivek Sharma
    Oct 31, 2005
  3. =?Utf-8?B?V291dGVy?=

    Dynamically Loaded controls loading other controls

    =?Utf-8?B?V291dGVy?=, Feb 27, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    484
    =?Utf-8?B?V291dGVy?=
    Feb 27, 2006
  4. Thea
    Replies:
    7
    Views:
    1,892
  5. Vivek Sharma

    Accessing the properties of a dynamically loaded control

    Vivek Sharma, Oct 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    84
    Phillip Williams
    Oct 31, 2005
Loading...

Share This Page