473,503 Members | 3,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Load User Control Dynamically, Cast object dynamically

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
1 6253
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3977
by: TIM T | last post by:
I want to load a user control from a dll dynamically to my main form. I have been reading about using reflection. I got this to work with the methods from my other classes in the dll but I am not...
9
21626
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
11
3098
by: Wolfgang Kaml | last post by:
I am not sure if this is more of an expert question, but I am sure that they are out there. I'd like to setup a general application or bin directory on my Win2003.Net Server that will hold some...
1
1022
by: Reza Nabi | last post by:
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...
7
3176
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
6
2058
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
2
1784
by: A.Wussow | last post by:
Hi Everybody, i want to load dynamically content from some user controls (with forms, or some data-controls) using atlas. So i use an UpdatePanel for loading the user control into a placeholder....
3
1980
by: Ronald S. Cook | last post by:
I lost my last post, but I'm looking for a better way to manage loading user controls (that are essentially forms) in a Windows app. In my current design, I have a ListBar in which the user...
9
18003
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.