Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Load User Control Dynamically, Cast object dynamically

Reply
Thread Tools

Load User Control Dynamically, Cast object dynamically

 
 
=?Utf-8?B?UmV6YSBOYWJp?=
Guest
Posts: n/a
 
      03-04-2005
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 ----


 
Reply With Quote
 
 
 
 
Scott Allen
Guest
Posts: n/a
 
      03-04-2005
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 <(E-Mail Removed)>
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 ----
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to dynamically cast a user control sbilbrey ASP .Net 0 04-28-2009 07:51 PM
Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object Phoenix ASP .Net Building Controls 2 06-06-2005 04:02 PM
Dynamically load user control? Green ASP .Net 1 05-12-2005 12:23 AM
Load User Control Dynamically, Cast object dynamically =?Utf-8?B?UmV6YSBOYWJp?= ASP .Net 1 03-05-2005 12:04 AM
Dynamically Adding User Control with Child User Control Travis Pruitt ASP .Net Building Controls 0 01-13-2005 08:49 PM



Advertisments