How to access controls

Discussion in 'ASP .Net Building Controls' started by Skeeve, Sep 25, 2003.

  1. Skeeve

    Skeeve Guest

    I am creating controls dynamically (programmatically) in a loop that
    is navigating through a XML DOM.
    Once I (the user) submit the form and reloads the page (or with
    AutoPostBack), how can I access the controls (i.e. DropDownBoxes) to
    get the value?

    Since the Dropdownboxes are created in a loop, they all have the same
    name don't they? I have no idea at this point how I can access the
    individual controls.

    Code_Snippet:

    Dim DropDownBox As New DropDownList()
    DropDownBox.AutoPostBack = True
    tCell.Controls.Add(DropDownBox)
    datas = element.childNodes
    For Each data In datas
    DropDownBox_String = data.lastChild.xml & " - " &
    data.attributes.item(0).nodeValue
    DropDownBox.Items.Add(DropDownBox_String)
    Next data

    Thanks,
    Torsten
     
    Skeeve, Sep 25, 2003
    #1
    1. Advertising

  2. "Skeeve" <> wrote in message
    news:...
    > I am creating controls dynamically (programmatically) in a loop that
    > is navigating through a XML DOM.
    > Once I (the user) submit the form and reloads the page (or with
    > AutoPostBack), how can I access the controls (i.e. DropDownBoxes) to
    > get the value?
    >
    > Since the Dropdownboxes are created in a loop, they all have the same
    > name don't they? I have no idea at this point how I can access the
    > individual controls.
    >
    > Code_Snippet:
    >
    > Dim DropDownBox As New DropDownList()
    > DropDownBox.AutoPostBack = True
    > tCell.Controls.Add(DropDownBox)
    > datas = element.childNodes
    > For Each data In datas
    > DropDownBox_String = data.lastChild.xml & " - " &
    > data.attributes.item(0).nodeValue
    > DropDownBox.Items.Add(DropDownBox_String)
    > Next data


    Torsten,

    When you are adding a variable number of controls to the same container, and
    they all have the same name, you want the container to implement the
    INamingContainer interface. That way, when you set the ID property of one of
    the added controls, the ClientID will be prefixed by the ID of the
    containing control. Because DataGridItem, DataListItem and RepeaterItem all
    implement this interface, controls you place in the templates of the
    DataGrid, DataList or Repeater controls all have their ID's prefixed.

    Now, if you needed to, you could create your own container which does this
    by creating a NamingContainer control:

    public class NamingContainer : PlaceHolder, INamingContainer
    {
    }

    then:

    Dim nc as New NamingContainer()
    tCell.Controls.Add(nc)
    Dim DropDownBox As New DropDownList()
    DropDownBox.AutoPostBack = True
    nc.Controls.Add(DropDownBox)
    ....

    I haven't tried this myself, so please let me know if it works!
    --
    John Saunders
    Internet Engineer
     
    John Saunders, Sep 25, 2003
    #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. trinitypete

    HTML Controls and User controls

    trinitypete, Jun 25, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    3,396
    trinitypete
    Jun 26, 2003
  2. Mr Newbie
    Replies:
    3
    Views:
    500
    Kevin Spencer
    Oct 31, 2005
  3. Nathan Sokalski
    Replies:
    4
    Views:
    1,150
    Muhammad Naveed Yaseen
    Sep 5, 2007
  4. Faizan Ahmed
    Replies:
    1
    Views:
    288
    John Saunders
    Jan 4, 2005
  5. Faizan Ahmed
    Replies:
    1
    Views:
    366
    John Saunders
    Jan 4, 2005
Loading...

Share This Page