Dynamicly loading server or user controls

T

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\\framework\\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.
 
M

Matt Berther

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);
 
K

Karl Seguin

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 said:
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\\framework\\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.
 
K

Kevin Spencer

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
 
K

Kevin Spencer

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 said:
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 said:
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\\framework\\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.
 
M

Matt Berther

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

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\\framework\\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.
 
K

Karl Seguin

Thanks, makes sense.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Matt Berther said:
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

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\\framework\\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.
 
B

bruce barker

easy as pie, try:

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

-- bruce (sqlwork.com)
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top