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

T

TCook

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
 
K

Kumar Reddi

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...
}
 
T

TCook

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 said:
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...
}
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
 
K

Kumar Reddi

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
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 said:
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...
}
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
 
T

TCook

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 said:
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
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 said:
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
 
T

TCook

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 said:
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
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 said:
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
 
K

Kumar Reddi

I had never worked with IE automation, so I have no idea as how it
works with asp.net
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 said:
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
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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top