[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webcontrols

Dynamically create web controls from a string value

geoff

3/1/2004 1:42:00 PM

Anyone know how to create a asp.net WebControl from a string value?
I'm pulling string values (eg. System.Web.UI.WebControls.TextBox) from
an XML file and would like to dynamically create a TextBox, then add
to Page.

I've tried creating with Activator.CreateInstance, but always returns
an "Specified cast is not valid" exception.

I've tried several variations of the following:
Sample #1-----------------

private void Page_Load(object sender, System.EventArgs e)
{
string controlName = "System.Web.UI.WebControls.TextBox";
Object o = Activator.CreateInstanceFrom(@"c:\windows\assembly\gac\system.web\1.0.3300.0__b03f5f7f11d50a3a\system.web.dll",controlName);
Control c = (Control)o;
Page.Controls.Add(c);
}

returns Exception = "Specified cast is not valid"


Sample #2-----------------

private void Page_Load(object sender, System.EventArgs e)
{
string controlName = "System.Web.UI.WebControls.TextBox";
Type t = Type.GetType(controlName);
Object o = Activator.CreateInstance(t);
Control c = (Control)o;
Page.Controls.Add(c);
}

returns Exception = "Value cannot be null. Parameter name: type"


I'm stabbing in the dark here, so any help is very much appreciated.

..geoff
2 Answers

geoff

3/1/2004 8:16:00 PM

0

Never mind. I figured it out.

private void Page_Load(object sender, System.EventArgs e)
{
string controlType = "System.Web.UI.WebControls.TextBox, System.Web,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
Type t = Type.GetType(controlType);

Control h = (Control)Activator.CreateInstance(t);
PlaceHolder1.Controls.Add(h);
}

--geoff


geoff@basicframe.com (basicframe) wrote in message news:<238dd61.0403010542.5bf7a24b@posting.google.com>...
> Anyone know how to create a asp.net WebControl from a string value?
> I'm pulling string values (eg. System.Web.UI.WebControls.TextBox) from
> an XML file and would like to dynamically create a TextBox, then add
> to Page.
>
> I've tried creating with Activator.CreateInstance, but always returns
> an "Specified cast is not valid" exception.
>
> I've tried several variations of the following:
> Sample #1-----------------
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> string controlName = "System.Web.UI.WebControls.TextBox";
> Object o = Activator.CreateInstanceFrom(@"c:\windows\assembly\gac\system.web\1.0.3300.0__b03f5f7f11d50a3a\system.web.dll",controlName);
> Control c = (Control)o;
> Page.Controls.Add(c);
> }
>
> returns Exception = "Specified cast is not valid"
>
>
> Sample #2-----------------
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> string controlName = "System.Web.UI.WebControls.TextBox";
> Type t = Type.GetType(controlName);
> Object o = Activator.CreateInstance(t);
> Control c = (Control)o;
> Page.Controls.Add(c);
> }
>
> returns Exception = "Value cannot be null. Parameter name: type"
>
>
> I'm stabbing in the dark here, so any help is very much appreciated.
>
> .geoff

v-jetan

3/2/2004 1:50:00 AM

0


Hi geoff,

I am glad you have found out the solution.

Actually, in Type.GetType() document, you will see that:
"If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly."

System.Web.UI.WebControls.TextBox is not a type in your assembly or in
mscorlib.dll, so you should specify the assembly name(System.Web).

Hope this information make sense to you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.