Dynamically create web controls from a string value

Discussion in 'ASP .Net Web Controls' started by basicframe, Mar 1, 2004.

  1. basicframe

    basicframe Guest

    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
    basicframe, Mar 1, 2004
    #1
    1. Advertising

  2. basicframe

    basicframe Guest

    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


    (basicframe) wrote in message news:<>...
    > 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
    basicframe, Mar 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msnews.microsoft.com

    How to Create Web Controls using ASP.NET Controls ?

    msnews.microsoft.com, Apr 27, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    6,212
    msnews.microsoft.com
    Apr 27, 2005
  2. Bob Rock
    Replies:
    1
    Views:
    606
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  3. Nathan Sokalski
    Replies:
    5
    Views:
    978
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  4. Nathan Sokalski
    Replies:
    4
    Views:
    260
    Nathan Sokalski
    Dec 21, 2006
  5. Nathan Sokalski
    Replies:
    4
    Views:
    299
    Nathan Sokalski
    Dec 21, 2006
Loading...

Share This Page