Load User Control Dynamically, Cast object dynamically

G

Guest

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 ----
 
M

Matt Berther

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 ----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top