Adding Controls (*.ascx) dynamically

Discussion in 'ASP .Net' started by C Downey, Nov 3, 2004.

  1. C Downey

    C Downey Guest

    I have a registration form that should load different web user controls
    based on the selection of a dropdown list. Basically, the user follows the
    following steps:

    1. user comes to very basic web form, no controls loaded
    2. user selects registration type (1 of 4 possible types) from a dropdown
    control
    3. the dropdown properties are :
    autopostback=True OnSelectedIndexChanged="displayUserGroupControl"
    4. Now when my displayUserGroupControl fires I would like to load the
    control based on their selection.

    What is the best way to do this? When the user submits the form I will
    create a new "user" record in my database as well as new records in other
    tables based on their UserGroup control. Is it fairly easy to get
    dynamically loaded controls to talk to each other?

    Any tips or best practices would be much appreciated. If I have to, I can
    change all the controls to tables in my main form and just hide them based
    on the user selection but then the code gets a little hard to read.

    Heres what I have right now - I want to confirm that Im doing this the best
    way before heading down the wrong road.

    Public Sub displayUserGroupControl(ByVal sender As System.Object, ByVal e
    As System.EventArgs)

    Dim tblName As String
    Dim selectValue As String = lbUserType.SelectedItem.Value

    Select Case selectValue

    Case "1"

    phProfilesAndInterests.Controls.Add(LoadControl("controls/register_practitio
    ner.ascx"))
    Case "2"

    phProfilesAndInterests.Controls.Add(LoadControl("controls/register_student.a
    scx"))
    Case ...

    End Select

    ' Response.Write(selectValue.ToString + "|||<BR>")

    End Sub
    C Downey, Nov 3, 2004
    #1
    1. Advertising

  2. C Downey

    Matt Berther Guest

    Hello C,

    I have an implementation that I discuss on a blog entry: http://www.mattberther.com/2004/10/000559.html

    This may work for you...

    --
    Matt Berther
    http://www.mattberther.com

    > I have a registration form that should load different web user
    > controls based on the selection of a dropdown list. Basically, the
    > user follows the following steps:
    >
    > 1. user comes to very basic web form, no controls loaded
    > 2. user selects registration type (1 of 4 possible types) from a
    > dropdown
    > control
    > 3. the dropdown properties are :
    > autopostback=True
    > OnSelectedIndexChanged="displayUserGroupControl"
    > 4. Now when my displayUserGroupControl fires I would like to load the
    > control based on their selection.
    >
    > What is the best way to do this? When the user submits the form I
    > will create a new "user" record in my database as well as new records
    > in other tables based on their UserGroup control. Is it fairly easy
    > to get dynamically loaded controls to talk to each other?
    >
    > Any tips or best practices would be much appreciated. If I have to, I
    > can change all the controls to tables in my main form and just hide
    > them based on the user selection but then the code gets a little hard
    > to read.
    >
    > Heres what I have right now - I want to confirm that Im doing this the
    > best way before heading down the wrong road.
    >
    > Public Sub displayUserGroupControl(ByVal sender As System.Object,
    > ByVal e As System.EventArgs)
    >
    > Dim tblName As String
    > Dim selectValue As String = lbUserType.SelectedItem.Value
    > Select Case selectValue
    >
    > Case "1"
    >
    > phProfilesAndInterests.Controls.Add(LoadControl("controls/register_pra
    > ctitio
    > ner.ascx"))
    > Case "2"
    > phProfilesAndInterests.Controls.Add(LoadControl("controls/register_stu
    > dent.a
    > scx"))
    > Case ...
    > End Select
    >
    > ' Response.Write(selectValue.ToString + "|||<BR>")
    >
    > End Sub
    >
    Matt Berther, Nov 3, 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. Quentin
    Replies:
    1
    Views:
    485
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    482
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,940
    Holger (David) Wagner
    Jul 3, 2004
  4. Frank
    Replies:
    2
    Views:
    411
    Victor Garcia Aprea [MVP]
    Nov 26, 2003
  5. Joe
    Replies:
    1
    Views:
    372
    Jeffrey Tan[MSFT]
    Feb 16, 2004
Loading...

Share This Page