473,408 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Dynamicly loading server or user controls

Tim
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do this
for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] + ".ascx");
this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was trying
to do the same thing. How ever his example did not work.

Assembly ass = Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\\System.Web.dll");

Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to be
loading the controls based on a result set from the database.
Nov 18 '05 #1
7 1549
Hello Tim,

I discuss an approach on my website at: http://www.mattberther.com/2004/10/000559.html

The way to do this using the pattern I describe would be to implement an
IControlFactory object that knows how to create the control.

This class would then do something like:

Type t = Type.GetType("System.Web.UI.WebControls.TextBox", true);
return (Control)Activator.CreateInstance(t);

--
Matt Berther
http://www.mattberther.com
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do this
for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] + ".ascx");
this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was trying
to do the same thing. How ever his example did not work.

Assembly ass =
Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\\S
ystem.Web.dll");

Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to be
loading the controls based on a result set from the database.

Nov 18 '05 #2
I'm a little confused. Adding a Server Control is not the same thing as
adding a User Control (ascx) by a long shot. A Server Control has no
Template; it is merely a class. Therefore, all you need to do is create an
instance of the class and add it to the Controls Collection of the
PlaceHolder. Example:

DropDownList ddl = new DropDownList();
// Initialize and populate the Control, then...
plcContent.Controls.Add(ddl);

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Tim" <tb****@hotmail.com> wrote in message
news:d4**************************@posting.google.c om...
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do this
for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] + ".ascx");
this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was trying
to do the same thing. How ever his example did not work.

Assembly ass = Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\\System.
Web.dll");
Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to be
loading the controls based on a result set from the database.

Nov 18 '05 #3
Matt,
I was going to suggest that but tried it out first....it doesn't seem to
work. I can't seem to GetType() on something in the GAC unless its in
system.dll....

I tried

"System.Web.UI.WebControls.TextBox, System.Web"
or
"System.Web.UI.WebControls.TextBox"

and no go....

maybe it's just me, I'm super tired....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Matt Berther" <mb******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP14.phx.gbl...
Hello Tim,

I discuss an approach on my website at: http://www.mattberther.com/2004/10/000559.html
The way to do this using the pattern I describe would be to implement an
IControlFactory object that knows how to create the control.

This class would then do something like:

Type t = Type.GetType("System.Web.UI.WebControls.TextBox", true);
return (Control)Activator.CreateInstance(t);

--
Matt Berther
http://www.mattberther.com
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do this
for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] + ".ascx");
this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was trying
to do the same thing. How ever his example did not work.

Assembly ass =
Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\\S
ystem.Web.dll");

Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to be
loading the controls based on a result set from the database.


Nov 18 '05 #4
Upon re-reading the post, I can see that this isn't the correct answer. I
didn't realize that you wanted to store strings and convert them to objects.
Using reflection to derive the Control type from a string would be pretty
expensive. I would suggest an enumeration that indicates the type of the
Control. You can store the values as integers. Then you can use a simple
switch statement to create an instance of the Control that you need. Just
one approach.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:ui**************@TK2MSFTNGP11.phx.gbl...
I'm a little confused. Adding a Server Control is not the same thing as
adding a User Control (ascx) by a long shot. A Server Control has no
Template; it is merely a class. Therefore, all you need to do is create an
instance of the class and add it to the Controls Collection of the
PlaceHolder. Example:

DropDownList ddl = new DropDownList();
// Initialize and populate the Control, then...
plcContent.Controls.Add(ddl);

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Tim" <tb****@hotmail.com> wrote in message
news:d4**************************@posting.google.c om...
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do this
for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] + ".ascx");
this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was trying
to do the same thing. How ever his example did not work.

Assembly ass =

Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\\System. Web.dll");

Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to be
loading the controls based on a result set from the database.


Nov 18 '05 #5
Hello Karl,

Of course... It cant resolve a type in the GAC unless you specify the fully
qualified type. This makes sense as the user could have two versions of the
framework installed. Because of this, you need to specify what you want for
GAC assemblies...

Type type = Type.GetType("System.Web.UI.WebControls.TextBox, System.Web,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true);
return (Control)Activator.CreateInstance(type);

This *does* work...

--
Matt Berther
http://www.mattberther.com
Matt,
I was going to suggest that but tried it out first....it doesn't seem
to
work. I can't seem to GetType() on something in the GAC unless its in
system.dll....
I tried

"System.Web.UI.WebControls.TextBox, System.Web"
or
"System.Web.UI.WebControls.TextBox"
and no go....

maybe it's just me, I'm super tired....

Karl

"Matt Berther" <mb******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP14.phx.gbl...
Hello Tim,

I discuss an approach on my website at:

http://www.mattberther.com/2004/10/000559.html
The way to do this using the pattern I describe would be to implement
an IControlFactory object that knows how to create the control.

This class would then do something like:

Type t = Type.GetType("System.Web.UI.WebControls.TextBox", true);
return (Control)Activator.CreateInstance(t);

--
Matt Berther
http://www.mattberther.com
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do
this for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] +
".ascx"); this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was
trying to do the same thing. How ever his example did not work.

Assembly ass =
Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\
\S ystem.Web.dll");

Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to
be loading the controls based on a result set from the database.

Nov 18 '05 #6
easy as pie, try:

WebControl ctrl = (WebControl)
Activator.CreateInstance("System.Web.UI.WebControl s", "TextBox");

-- bruce (sqlwork.com)
"Tim" <tb****@hotmail.com> wrote in message
news:d4**************************@posting.google.c om...
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do this
for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] + ".ascx");
this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was trying
to do the same thing. How ever his example did not work.

Assembly ass = Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\\System.
Web.dll");
Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to be
loading the controls based on a result set from the database.

Nov 18 '05 #7
Thanks, makes sense.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Matt Berther" <mb******@hotmail.com> wrote in message
news:es****************@TK2MSFTNGP15.phx.gbl...
Hello Karl,

Of course... It cant resolve a type in the GAC unless you specify the fully qualified type. This makes sense as the user could have two versions of the framework installed. Because of this, you need to specify what you want for GAC assemblies...

Type type = Type.GetType("System.Web.UI.WebControls.TextBox, System.Web,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true); return (Control)Activator.CreateInstance(type);

This *does* work...

--
Matt Berther
http://www.mattberther.com
Matt,
I was going to suggest that but tried it out first....it doesn't seem
to
work. I can't seem to GetType() on something in the GAC unless its in
system.dll....
I tried

"System.Web.UI.WebControls.TextBox, System.Web"
or
"System.Web.UI.WebControls.TextBox"
and no go....

maybe it's just me, I'm super tired....

Karl

"Matt Berther" <mb******@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP14.phx.gbl...
Hello Tim,

I discuss an approach on my website at:

http://www.mattberther.com/2004/10/000559.html
The way to do this using the pattern I describe would be to implement
an IControlFactory object that knows how to create the control.

This class would then do something like:

Type t = Type.GetType("System.Web.UI.WebControls.TextBox", true);
return (Control)Activator.CreateInstance(t);

--
Matt Berther
http://www.mattberther.com
I am trying to load both server and user controls into placeholder
controls on a aspx template page at runtime. These values would be
strings that are returned from a database query. I know I can do
this for user controls easily using:

oContent = (ControlBase) Page.LoadControl(Session["Page"] +
".ascx"); this.plcContent.Controls.Add(oContent);

but can not figure out how to do it for server controls. since I do
not know what the control will be that will be on the page at design
time I do not just want to create a bunch of controls then loop
through them to return that type.

the closest I have found on the web was where someone else was
trying to do the same thing. How ever his example did not work.

Assembly ass =
Assembly.LoadFrom("c:\\windows\\microsoft.net\\fra mework\\v1.1.4322\
\S ystem.Web.dll");

Type typ = ass.GetType("System.Web.UI.WebControls.TextBox", true,
false);

ConstructorInfo ci = typ.GetConstructor (System.Type.EmptyTypes);

WebControl ctrl = (WebControl)ci.Invoke(null);

this.plcContent.Controls.Add(ctrl);

this code blows during the invoke method.

Can someone help me here and also tell me if this is a bad idea to
be loading the controls based on a result set from the database.


Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: paula | last post by:
I've got a problem with asp.net i am trying to make a menu control. and have searched the web for serveral controls but they don't work correctly. I am pretty new to asp.net building. What am i...
5
by: Henke | last post by:
Hi! I add some web user controls dynamicly with: myPanel.Controls.Add(Page.LoadControl("MyDynamiclyAddedPage.ascx")); the style of the controls on the user control is set from a css-class, by...
0
by: Ben | last post by:
Hi I am using this code to dynamically load a user control: Dim path As String = "topbar_all.ascx" Dim ctl As UserControl = CType(Page.LoadControl(path), UserControl) Page.Controls.Add(ctl) ...
1
by: Henke | last post by:
Hi I have a aspx-page with a panel-control. On this panel control I add user controls dynamicly with LoadControl and panel.Controls.Add(myControl). On some of the dynamicly added user controls I...
2
by: DaWoE | last post by:
Hi all, I'm fairly new to ASP.NET. What i want to do is creat a online registration form. On the first step is getting the users details and the number of people he wants to register. Based on...
2
by: Dave A | last post by:
I just don't get this... If I need to dynamically load controls into a web page I simply need to go PlaceHolder1.Controls.Add(new Button()); or similar. However when I need to dynamically...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
2
by: Wayne Sepega | last post by:
I have the following Code: foreach (MyClass d in myClassCollection) { curRadioBtn = new RadioButton(); curRadioBtn.Text = d.ToString(); curRadioBtn.GroupName = "Contact"; curRadioBtn.ID =...
2
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I have a web app that when I click a menu, I load a control. On one of these controls, I have a button that when clicked, I need to unload the control to unload itself and load a different control...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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...

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.