B
basicframe
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
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