Load User Control Dynamically, Cast object dynamically

Discussion in 'ASP .Net' started by =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005.

  1. Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a
    placeholder dynamically based on the ctlName querystring passed in the URL.
    Webform (LoadCtl.aspx) also passes a variable (targetId) in to the
    usercontrol (IntergySite.aspx) by calling its setter method.

    Currently, I am using if-then-else and hardcoded the User Control Object to
    do casting and call the setter method.

    Question: Is there any way I could load, create user control object
    dynamically and call the setter method of usercontrol gracefully from the
    webform?

    Please find LoadCtl.aspx.cs (code-behind of webform) below:

    Thank you in advance for any pointer/help.

    Reza Nabi


    -------BEGIN LoadCtl.aspx------
    using System;
    using System.Data;
    using RMS.Lib.WebControl;
    using RMS.Lib.DAO;

    namespace RMS
    {
    /// <summary>
    /// Summary description for LoadCtl.
    /// </summary>
    public class LoadCtl : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.PlaceHolder phSites;


    private void Page_Load(object sender, System.EventArgs e)
    {

    string ctlName = Request.QueryString["ctlName"];
    int targetId = Convert.ToInt32(Request.QueryString["tid"]);

    if(!Page.IsPostBack)
    {
    /*
    QUESTION: Can we get rid of the following if then else and dynamically
    load user control based on the ctlName variable and dynamically cast
    the object as the
    ctlName? The reason I need to cast dynamically is I was passing
    targetId from aspx page to the
    user control.

    I want to do something like the following, dynamically set the ? marks
    at runtime
    Is there any way to do that?

    Type theType = Type.GetType("RMS.Lib.WebControl."+ctlName+".ascx");
    (????) theObj = (????) Activator.CreateInstance(theType);
    // QUESTION: Is there any way to cast the object at runtime dynamically
    based on the
    // ctlName variable passed on the URL?

    theObj.TargetId = targetId;
    phSites.Controls.Add(theObj);

    */
    if (ctlName.Equals("IntergySite"))
    {
    IntergySite s = (IntergySite)
    Page.LoadControl("Lib/WebControl/IntergySite.ascx");
    s.TargetId = targetId;// this is where I was passing targetId from aspx
    page to the control.
    phSites.Controls.Add(s);
    }
    else if (ctlName.Equals("UlitaSite"))
    {
    UlitaSite s = (UlitaSite)
    Page.LoadControl("Lib/WebControl/UlitaSite.ascx");
    s.TargetId = targetId;// this is where I was passing targetId from aspx
    page to the control.
    phSites.Controls.Add(s);
    }
    // here goes more ugly if statement :)
    else
    {
    DefaultSite s = (DefaultSite)
    Page.LoadControl("Lib/WebControl/GenericSite.ascx");
    s.TargetId = targetId;// this is where I was passing targetId from aspx
    page to the control.
    phSites.Controls.Add(s);
    }

    }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
    }

    -----END LoadCtl.aspx.cs ----
    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?UmV6YSBOYWJp?=

    Matt Berther Guest

    Hello Reza,

    It appears that all of your controls have one thing in common which is the
    TargetId. So, to start with, Id create a base class that includes this and
    then derive your controls from this new base class.

    [C#]
    public class MyBaseControl : UserControl
    {
    private string targetId;
    public string TargetId
    {
    get { return targetId; }
    set { targetId = value; }
    }
    }

    Secondly, you'll still need to do a LoadControl to add the control to the
    page, so you could try something like this:

    string controlPath = "Lib/WebControl/" + ctlName + ".ascx";
    MyBaseControl control = Page.LoadControl(controlPath) as MyBaseControl;
    control.TargetId = targetId;
    phSites.Controls.Add(control);

    --
    Matt Berther
    http://www.mattberther.com

    > Bakground: I have a webform (LoadCtl.aspx) which loads the user
    > control to a placeholder dynamically based on the ctlName querystring
    > passed in the URL. Webform (LoadCtl.aspx) also passes a variable
    > (targetId) in to the usercontrol (IntergySite.aspx) by calling its
    > setter method.
    >
    > Currently, I am using if-then-else and hardcoded the User Control
    > Object to do casting and call the setter method.
    >
    > Question: Is there any way I could load, create user control object
    > dynamically and call the setter method of usercontrol gracefully from
    > the webform?
    >
    > Please find LoadCtl.aspx.cs (code-behind of webform) below:
    >
    > Thank you in advance for any pointer/help.
    >
    > Reza Nabi
    >
    > -------BEGIN LoadCtl.aspx------
    > using System;
    > using System.Data;
    > using RMS.Lib.WebControl;
    > using RMS.Lib.DAO;
    > namespace RMS
    > {
    > /// <summary>
    > /// Summary description for LoadCtl.
    > /// </summary>
    > public class LoadCtl : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.PlaceHolder phSites;
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > string ctlName = Request.QueryString["ctlName"];
    > int targetId = Convert.ToInt32(Request.QueryString["tid"]);
    > if(!Page.IsPostBack)
    > {
    > /*
    > QUESTION: Can we get rid of the following if then else and
    > dynamically
    > load user control based on the ctlName variable and dynamically
    > cast
    > the object as the
    > ctlName? The reason I need to cast dynamically is I was passing
    > targetId from aspx page to the
    > user control.
    > I want to do something like the following, dynamically set the ?
    > marks
    > at runtime
    > Is there any way to do that?
    > Type theType =
    > Type.GetType("RMS.Lib.WebControl."+ctlName+".ascx");
    > (????) theObj = (????) Activator.CreateInstance(theType);
    > // QUESTION: Is there any way to cast the object at runtime
    > dynamically
    > based on the
    > // ctlName variable passed on the URL?
    > theObj.TargetId = targetId;
    > phSites.Controls.Add(theObj);
    > */
    > if (ctlName.Equals("IntergySite"))
    > {
    > IntergySite s = (IntergySite)
    > Page.LoadControl("Lib/WebControl/IntergySite.ascx");
    > s.TargetId = targetId;// this is where I was passing targetId
    > from aspx
    > page to the control.
    > phSites.Controls.Add(s);
    > }
    > else if (ctlName.Equals("UlitaSite"))
    > {
    > UlitaSite s = (UlitaSite)
    > Page.LoadControl("Lib/WebControl/UlitaSite.ascx");
    > s.TargetId = targetId;// this is where I was passing targetId
    > from aspx
    > page to the control.
    > phSites.Controls.Add(s);
    > }
    > // here goes more ugly if statement :)
    > else
    > {
    > DefaultSite s = (DefaultSite)
    > Page.LoadControl("Lib/WebControl/GenericSite.ascx");
    > s.TargetId = targetId;// this is where I was passing targetId
    > from aspx
    > page to the control.
    > phSites.Controls.Add(s);
    > }
    > }
    > }
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    > #endregion
    > }
    > }
    > -----END LoadCtl.aspx.cs ----
    >
    Matt Berther, Mar 5, 2005
    #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?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,337
    Scott Allen
    Mar 4, 2005
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,055
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    615
    Martin Dickopp
    Feb 14, 2004
  4. sbilbrey
    Replies:
    0
    Views:
    539
    sbilbrey
    Apr 28, 2009
  5. Henry
    Replies:
    1
    Views:
    151
    Henry
    Oct 20, 2003
Loading...

Share This Page