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