Loop through 'option' collection of a 'select' control?

Discussion in 'ASP .Net' started by TCook, Aug 13, 2006.

  1. TCook

    TCook Guest

    Hello,

    I was wondering if anyone has a code snippet for looping through a 'select'
    control's 'option' elements?

    Do I have to use an ASP.Net web control such as an asp list control or
    dropdown to do such a thing?

    Thanks,

    TC
    TCook, Aug 13, 2006
    #1
    1. Advertising

  2. TCook

    Kumar Reddi Guest

    It should be quite simple to do the looping in javascript for a
    "select" control..

    You can do the looping with a simple for loop

    for (i=0; i <= document.forms[0].SelectControlID.options.length - 1;
    i++)
    {
    //do whatever...
    }

    TCook wrote:
    > Hello,
    >
    > I was wondering if anyone has a code snippet for looping through a 'select'
    > control's 'option' elements?
    >
    > Do I have to use an ASP.Net web control such as an asp list control or
    > dropdown to do such a thing?
    >
    > Thanks,
    >
    > TC
    Kumar Reddi, Aug 13, 2006
    #2
    1. Advertising

  3. TCook

    TCook Guest

    Hey Kumar,

    I'm actually trying the 'submit' button's 'Click' event and trying to use
    VB.Net in the code behind class as follows:
    For Each ThisItem In Me.MySelect.Items
    If ThisItem.Selected = True Then
    ' Do something
    End If
    Next

    I am able to read other controls this way but my 'select' control is showing
    as not having any items present even though there are when it is displayed.

    Any thoughts?

    Todd


    "Kumar Reddi" <> wrote in message
    news:...
    > It should be quite simple to do the looping in javascript for a
    > "select" control..
    >
    > You can do the looping with a simple for loop
    >
    > for (i=0; i <= document.forms[0].SelectControlID.options.length - 1;
    > i++)
    > {
    > //do whatever...
    > }
    >
    > TCook wrote:
    >> Hello,
    >>
    >> I was wondering if anyone has a code snippet for looping through a
    >> 'select'
    >> control's 'option' elements?
    >>
    >> Do I have to use an ASP.Net web control such as an asp list control or
    >> dropdown to do such a thing?
    >>
    >> Thanks,
    >>
    >> TC

    >
    TCook, Aug 13, 2006
    #3
  4. TCook

    Kumar Reddi Guest

    I am guessing you have a HTML select control with runat=server. Even if
    its not a HTML server control.. the only thing that stops the page from
    remembering the items is the viewstate. If its the HTML server control,
    see if the viewstate for the page is turned off. If the select control
    is a asp.net webcontrol, see if the viewstate for the control or the
    page is turned off..

    Also, make sure you are loading the data into the control only once
    during the page load.. but not during page postback also, which would
    reset the control's items.. so, in that case the selected item value is
    lost by the time you check its value in the button click event handler

    TCook wrote:
    > Hey Kumar,
    >
    > I'm actually trying the 'submit' button's 'Click' event and trying to use
    > VB.Net in the code behind class as follows:
    > For Each ThisItem In Me.MySelect.Items
    > If ThisItem.Selected = True Then
    > ' Do something
    > End If
    > Next
    >
    > I am able to read other controls this way but my 'select' control is showing
    > as not having any items present even though there are when it is displayed.
    >
    > Any thoughts?
    >
    > Todd
    >
    >
    > "Kumar Reddi" <> wrote in message
    > news:...
    > > It should be quite simple to do the looping in javascript for a
    > > "select" control..
    > >
    > > You can do the looping with a simple for loop
    > >
    > > for (i=0; i <= document.forms[0].SelectControlID.options.length - 1;
    > > i++)
    > > {
    > > //do whatever...
    > > }
    > >
    > > TCook wrote:
    > >> Hello,
    > >>
    > >> I was wondering if anyone has a code snippet for looping through a
    > >> 'select'
    > >> control's 'option' elements?
    > >>
    > >> Do I have to use an ASP.Net web control such as an asp list control or
    > >> dropdown to do such a thing?
    > >>
    > >> Thanks,
    > >>
    > >> TC

    > >
    Kumar Reddi, Aug 13, 2006
    #4
  5. TCook

    TCook Guest

    Hey Kumar,

    I'm actually adding the 'option' list items via IE automation from the
    clientside dynamically. When the page is submitted, I am checking for items
    that are selected in the 'select' list. What's weird is that I'm also
    populating text input controls using IE automation and their values show up
    and are accessible. Below is the code that I use to create the 'option'
    items:

    For Each ThisItem In SomeItems
    lstDropDown = docDocument.getElementById("MySelect")
    optNewListItem = docDocument.createElement("option")
    optNewListItem.innerText = ThisItem .Name
    lstDropDown.appendChild(optNewListItem)
    Next

    Why aren't the 'select' controls working the same?

    Regards,

    Todd



    "Kumar Reddi" <> wrote in message
    news:...
    >I am guessing you have a HTML select control with runat=server. Even if
    > its not a HTML server control.. the only thing that stops the page from
    > remembering the items is the viewstate. If its the HTML server control,
    > see if the viewstate for the page is turned off. If the select control
    > is a asp.net webcontrol, see if the viewstate for the control or the
    > page is turned off..
    >
    > Also, make sure you are loading the data into the control only once
    > during the page load.. but not during page postback also, which would
    > reset the control's items.. so, in that case the selected item value is
    > lost by the time you check its value in the button click event handler
    >
    > TCook wrote:
    >> Hey Kumar,
    >>
    >> I'm actually trying the 'submit' button's 'Click' event and trying to use
    >> VB.Net in the code behind class as follows:
    >> For Each ThisItem In Me.MySelect.Items
    >> If ThisItem.Selected = True Then
    >> ' Do something
    >> End If
    >> Next
    >>
    >> I am able to read other controls this way but my 'select' control is
    >> showing
    >> as not having any items present even though there are when it is
    >> displayed.
    >>
    >> Any thoughts?
    >>
    >> Todd
    >>
    >>
    >> "Kumar Reddi" <> wrote in message
    >> news:...
    >> > It should be quite simple to do the looping in javascript for a
    >> > "select" control..
    >> >
    >> > You can do the looping with a simple for loop
    >> >
    >> > for (i=0; i <= document.forms[0].SelectControlID.options.length - 1;
    >> > i++)
    >> > {
    >> > //do whatever...
    >> > }
    >> >
    >> > TCook wrote:
    >> >> Hello,
    >> >>
    >> >> I was wondering if anyone has a code snippet for looping through a
    >> >> 'select'
    >> >> control's 'option' elements?
    >> >>
    >> >> Do I have to use an ASP.Net web control such as an asp list control or
    >> >> dropdown to do such a thing?
    >> >>
    >> >> Thanks,
    >> >>
    >> >> TC
    >> >

    >
    TCook, Aug 13, 2006
    #5
  6. TCook

    TCook Guest

    Hey Kumar,

    Just for clarity, it's not that it's showing as not being selected but
    rather as if it's empty (i.e. MySelectControl.Items.Count = 0)

    Regards,

    Todd


    "Kumar Reddi" <> wrote in message
    news:...
    >I am guessing you have a HTML select control with runat=server. Even if
    > its not a HTML server control.. the only thing that stops the page from
    > remembering the items is the viewstate. If its the HTML server control,
    > see if the viewstate for the page is turned off. If the select control
    > is a asp.net webcontrol, see if the viewstate for the control or the
    > page is turned off..
    >
    > Also, make sure you are loading the data into the control only once
    > during the page load.. but not during page postback also, which would
    > reset the control's items.. so, in that case the selected item value is
    > lost by the time you check its value in the button click event handler
    >
    > TCook wrote:
    >> Hey Kumar,
    >>
    >> I'm actually trying the 'submit' button's 'Click' event and trying to use
    >> VB.Net in the code behind class as follows:
    >> For Each ThisItem In Me.MySelect.Items
    >> If ThisItem.Selected = True Then
    >> ' Do something
    >> End If
    >> Next
    >>
    >> I am able to read other controls this way but my 'select' control is
    >> showing
    >> as not having any items present even though there are when it is
    >> displayed.
    >>
    >> Any thoughts?
    >>
    >> Todd
    >>
    >>
    >> "Kumar Reddi" <> wrote in message
    >> news:...
    >> > It should be quite simple to do the looping in javascript for a
    >> > "select" control..
    >> >
    >> > You can do the looping with a simple for loop
    >> >
    >> > for (i=0; i <= document.forms[0].SelectControlID.options.length - 1;
    >> > i++)
    >> > {
    >> > //do whatever...
    >> > }
    >> >
    >> > TCook wrote:
    >> >> Hello,
    >> >>
    >> >> I was wondering if anyone has a code snippet for looping through a
    >> >> 'select'
    >> >> control's 'option' elements?
    >> >>
    >> >> Do I have to use an ASP.Net web control such as an asp list control or
    >> >> dropdown to do such a thing?
    >> >>
    >> >> Thanks,
    >> >>
    >> >> TC
    >> >

    >
    TCook, Aug 14, 2006
    #6
  7. TCook

    Kumar Reddi Guest

    I had never worked with IE automation, so I have no idea as how it
    works with asp.net

    TCook wrote:
    > Hey Kumar,
    >
    > Just for clarity, it's not that it's showing as not being selected but
    > rather as if it's empty (i.e. MySelectControl.Items.Count = 0)
    >
    > Regards,
    >
    > Todd
    >
    >
    > "Kumar Reddi" <> wrote in message
    > news:...
    > >I am guessing you have a HTML select control with runat=server. Even if
    > > its not a HTML server control.. the only thing that stops the page from
    > > remembering the items is the viewstate. If its the HTML server control,
    > > see if the viewstate for the page is turned off. If the select control
    > > is a asp.net webcontrol, see if the viewstate for the control or the
    > > page is turned off..
    > >
    > > Also, make sure you are loading the data into the control only once
    > > during the page load.. but not during page postback also, which would
    > > reset the control's items.. so, in that case the selected item value is
    > > lost by the time you check its value in the button click event handler
    > >
    > > TCook wrote:
    > >> Hey Kumar,
    > >>
    > >> I'm actually trying the 'submit' button's 'Click' event and trying to use
    > >> VB.Net in the code behind class as follows:
    > >> For Each ThisItem In Me.MySelect.Items
    > >> If ThisItem.Selected = True Then
    > >> ' Do something
    > >> End If
    > >> Next
    > >>
    > >> I am able to read other controls this way but my 'select' control is
    > >> showing
    > >> as not having any items present even though there are when it is
    > >> displayed.
    > >>
    > >> Any thoughts?
    > >>
    > >> Todd
    > >>
    > >>
    > >> "Kumar Reddi" <> wrote in message
    > >> news:...
    > >> > It should be quite simple to do the looping in javascript for a
    > >> > "select" control..
    > >> >
    > >> > You can do the looping with a simple for loop
    > >> >
    > >> > for (i=0; i <= document.forms[0].SelectControlID.options.length - 1;
    > >> > i++)
    > >> > {
    > >> > //do whatever...
    > >> > }
    > >> >
    > >> > TCook wrote:
    > >> >> Hello,
    > >> >>
    > >> >> I was wondering if anyone has a code snippet for looping through a
    > >> >> 'select'
    > >> >> control's 'option' elements?
    > >> >>
    > >> >> Do I have to use an ASP.Net web control such as an asp list control or
    > >> >> dropdown to do such a thing?
    > >> >>
    > >> >> Thanks,
    > >> >>
    > >> >> TC
    > >> >

    > >
    Kumar Reddi, Aug 14, 2006
    #7
    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. J. Hall
    Replies:
    2
    Views:
    1,045
    Jeff Thies
    Apr 21, 2004
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    950
    Øyvind Isaksen
    May 18, 2007
  3. WebRaster
    Replies:
    1
    Views:
    260
    Francisco Spaeth
    Oct 26, 2005
  4. Nick Spacek

    Removing select option removes entire select

    Nick Spacek, Jun 21, 2006, in forum: Javascript
    Replies:
    1
    Views:
    141
    Matt Kruse
    Jun 21, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    354
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page