Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Loop through 'option' collection of a 'select' control?

Reply
Thread Tools

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

 
 
TCook
Guest
Posts: n/a
 
      08-13-2006
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


 
Reply With Quote
 
 
 
 
Kumar Reddi
Guest
Posts: n/a
 
      08-13-2006
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


 
Reply With Quote
 
 
 
 
TCook
Guest
Posts: n/a
 
      08-13-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

>



 
Reply With Quote
 
Kumar Reddi
Guest
Posts: n/a
 
      08-13-2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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

> >


 
Reply With Quote
 
TCook
Guest
Posts: n/a
 
      08-13-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>> > 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
>> >

>



 
Reply With Quote
 
TCook
Guest
Posts: n/a
 
      08-13-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>> > 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
>> >

>



 
Reply With Quote
 
Kumar Reddi
Guest
Posts: n/a
 
      08-14-2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >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" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed) oups.com...
> >> > 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
> >> >

> >


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Loop through a collection Fresno Bob ASP .Net 3 12-24-2008 08:09 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
STL - an algorithm for finding a collection within a collection? Dylan C++ 5 03-22-2005 01:31 AM



Advertisments