By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

Load User Control Dynamically, Cast object dynamically

P: n/a
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 ----
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Reza:

Have all of your user controls implement a common interface. This
guarantees you can cast any of them to the same interface and invoke a
method or property on the interface.

Karl has a good article covering this design:
http://openmymind.net/index.aspx?documentId=9
Whenever you see case statements like this, think about inheritance
and polymorphism.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 4 Mar 2005 11:36:32 -0800, Reza Nabi <rn***@hotmail.com>
wrote:
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 ----


Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.