(Hard one,any expert out threre ? ) Create object from string ? add webcontrols dynamically on page.

Discussion in 'ASP .Net' started by jesper_lofgren@yahoo.se, Dec 17, 2005.

  1. Guest

    Hi there,

    I have some webcontrols that i want to add dynamically on a page. I
    have stored the path / namespace in database (ex
    MyNameSpace.WebControls.Control1) to the class/webcontrol.

    Lets say i have a column in database that looks like this.

    PageID (int) Webcontrol (varchar)
    1 MyNameSpace.WebControls.Control1
    1 MyNameSpace.WebControls.Control2

    Then i want to add the controls dynamically on the page, anyone know
    how to do this ?
    I guess i have to create a instance of the webcontrol and the add it
    with Controls.add() . But how do i create the instance and add it
    dynamically from a string?

    Hope anyone can help me with this, some expert out there ?? :)

    Thanks
    Jesper
    , Dec 17, 2005
    #1
    1. Advertising

  2. Mike Gaab Guest

    <> wrote in message
    news:...
    > Hi there,
    >
    > I have some webcontrols that i want to add dynamically on a page. I
    > have stored the path / namespace in database (ex
    > MyNameSpace.WebControls.Control1) to the class/webcontrol.
    >
    > Lets say i have a column in database that looks like this.
    >
    > PageID (int) Webcontrol (varchar)
    > 1 MyNameSpace.WebControls.Control1
    > 1 MyNameSpace.WebControls.Control2
    >
    > Then i want to add the controls dynamically on the page, anyone know
    > how to do this ?
    > I guess i have to create a instance of the webcontrol and the add it
    > with Controls.add() . But how do i create the instance and add it
    > dynamically from a string?
    >
    > Hope anyone can help me with this, some expert out there ?? :)
    >


    I am not an expert, just beginning but it you might consider placing
    the controls on the webform in design mode with the visible property
    set to false, then at runtime simple set the visible property to true as
    needed.

    Mike



    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mike Gaab, Dec 17, 2005
    #2
    1. Advertising

  3. Karl Seguin Guest

    It's much easier if you have the path. Controls are templated and aren't
    instantiated like normal classes.

    Control c = Page.LoadControl("~/controls/login.ascx");
    container.Controls.Add(c);

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/
    http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



    <> wrote in message
    news:...
    > Hi there,
    >
    > I have some webcontrols that i want to add dynamically on a page. I
    > have stored the path / namespace in database (ex
    > MyNameSpace.WebControls.Control1) to the class/webcontrol.
    >
    > Lets say i have a column in database that looks like this.
    >
    > PageID (int) Webcontrol (varchar)
    > 1 MyNameSpace.WebControls.Control1
    > 1 MyNameSpace.WebControls.Control2
    >
    > Then i want to add the controls dynamically on the page, anyone know
    > how to do this ?
    > I guess i have to create a instance of the webcontrol and the add it
    > with Controls.add() . But how do i create the instance and add it
    > dynamically from a string?
    >
    > Hope anyone can help me with this, some expert out there ?? :)
    >
    > Thanks
    > Jesper
    >
    Karl Seguin, Dec 17, 2005
    #3
  4. Assuming that there is some good reason for using a database to dynamically
    load controls, I think you've gotten off on the wrong foot. Why are you
    storing strings in the database? The names of namespaces and classes is
    useful to us humans, but not particularly to computers. It would be more
    efficient to store integers in the database. Assuming (again) that you know
    what you're storing in the database, you could create Enumerations that
    represent different namespaces and classes, and store the enumerated values
    in the database. From there it's a simple matter of identifying the Control
    to load via its enumerated value.

    You would need some sort of mechanism to identify the Controls whose names
    are stored in the database anyway. You could do it with Reflection, but that
    would be expensive, and I see no benefit gained from it.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Hi there,
    >
    > I have some webcontrols that i want to add dynamically on a page. I
    > have stored the path / namespace in database (ex
    > MyNameSpace.WebControls.Control1) to the class/webcontrol.
    >
    > Lets say i have a column in database that looks like this.
    >
    > PageID (int) Webcontrol (varchar)
    > 1 MyNameSpace.WebControls.Control1
    > 1 MyNameSpace.WebControls.Control2
    >
    > Then i want to add the controls dynamically on the page, anyone know
    > how to do this ?
    > I guess i have to create a instance of the webcontrol and the add it
    > with Controls.add() . But how do i create the instance and add it
    > dynamically from a string?
    >
    > Hope anyone can help me with this, some expert out there ?? :)
    >
    > Thanks
    > Jesper
    >
    Kevin Spencer, Dec 17, 2005
    #4
  5. Guest

    Thanks for all answers!
    Kevin i did it your way. Works great thanks !

    // Jesper
    , Dec 17, 2005
    #5
    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. Support
    Replies:
    4
    Views:
    1,417
    Scott Allen
    Mar 24, 2005
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Any one expert in Autorun.inf

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Aug 24, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    316
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Aug 24, 2006
  3. tommy

    create programatically webcontrols - dynamically

    tommy, Aug 3, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    111
    tommy
    Aug 3, 2004
  4. ronnel.santiago

    bind values of dynamically loaded webcontrols to custom object

    ronnel.santiago, Oct 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    133
    ronnel.santiago
    Oct 4, 2004
  5. transkawa
    Replies:
    2
    Views:
    120
    transkawa
    Jun 23, 2010
Loading...

Share This Page