creating controls dynamically

Posted on 2004-11-29
Last Modified: 2010-04-23
I'm trying to create a control from a string in a database:

Dim asm As [Assembly] =

Dim typ As Type = asm.GetType("System.Web.UI.WebControls.TextBox", True,

Dim o As Object = Activator.CreateInstance(typ)

Me.Controls.Add(CType(o, Control))

I get "Specified cast is not valid." with the line of code directly above.
How do I/ Can I cast a type to a control?

Question by:paulscalf
    LVL 9

    Expert Comment

    I may have not paid enough attention but should this be change to?:

    Me.Controls.Add(CType(o, TextBox))


    Me.Controls.Add(CType(o, System.Web.UI.WebControls.TextBox))

    Author Comment

    "System.Web.UI.WebControls.TextBox" is a string being passed into the function therefore, neither of these will work. We are creating a data driven app. I'd like to pass in the name of a control, again as a string, e.g."System.Web.UI.WebControls.TextBox", and be able to create a control to add to the form or webform... Thanks.
    LVL 24

    Expert Comment

    by:Jeff Certain
    The issue is that you are returning a string, and passing it to get the type of an object... which will return string for your case.

    Try this approach.

    private function getTypeFromName(typeName as string) as type
    select case typeName
    case  "System.Web.UI.WebControls.TextBox"
      return GetType(textbox)
    end select
    end function

    Then, use this code...

    Dim typ As Type = asm.GetType(getTypeFromName(typeName), True,

    Dim o As Object = Activator.CreateInstance(typ)

    Me.Controls.Add(CType(o, Control))

    Author Comment

    I can't use a case. I would have to add to cases to the getTypeFromName function for each control I wanted to create. How do I/ Can I from only a string, "System.Web.UI.WebControls.TextBox" create a control and add it to a form or webform?
    LVL 24

    Accepted Solution

    I've had to do this in the past and couldn't find any way other than using a select statement to return the proper type. Although .Net supports the Type.ToString, there doesn't seem to be any way to do the reverse.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Deselect Grid view Row 4 28
    How to do in LINQ?? 18 45
    VB.NET Server side Javascript 8 43
    Merging PDFs with VB.NET 10 12
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now