Home All Groups Group Topic Archive Search About
Author
10 Nov 2005 12:40 AM
Jim
Let's say we have a drop down list (lstStates) of the 50 states. Is there
another way to pre select a state, when the page loads, besides
lstStates.SelectedValue = "NY" for example?

The reason I'm asking is because when a value not in the 50 state master
list (for example, lstStates.SelectedValue = "TR"), the page erros out with a
"Specified argument was out of the range of valid values" error. I know the
correct answer is "well don't pass in TR" but that's another matter.

Is there another property of the DropDownList where if it's not in the
master list, it simply goes to the first item in the list? Or is there a
way/trick to handle this so the page doesn't error out.

Thanks in advance,
Jim

Author
10 Nov 2005 12:40 AM
Phillip Williams
Dim li As ListItem
'This clears previous selections on the list
dropdownlist1.ClearSelection() 
'returns a reference to the listitem with value TR
li = dropdownlist1.Items.FindByValue("TR") 
'if no listitem has the value TR then we get nothing
If li Is Nothing Then  
    'set the first item as the selected item
    dropdownlist1.SelectedIndex = 0
Else
    li.Selected = True
End If
Show quoteHide quote
"Jim" wrote:

> Let's say we have a drop down list (lstStates) of the 50 states. Is there
> another way to pre select a state, when the page loads, besides
> lstStates.SelectedValue = "NY" for example?
>
> The reason I'm asking is because when a value not in the 50 state master
> list (for example, lstStates.SelectedValue = "TR"), the page erros out with a
> "Specified argument was out of the range of valid values" error. I know the
> correct answer is "well don't pass in TR" but that's another matter.
>
> Is there another property of the DropDownList where if it's not in the
> master list, it simply goes to the first item in the list? Or is there a
> way/trick to handle this so the page doesn't error out.
>
> Thanks in advance,
> Jim
Author
10 Nov 2005 2:06 PM
Jim
Nice..Thank you Phillip!

Show quoteHide quote
"Phillip Williams" wrote:

> Dim li As ListItem
> 'This clears previous selections on the list
> dropdownlist1.ClearSelection() 
> 'returns a reference to the listitem with value TR
> li = dropdownlist1.Items.FindByValue("TR") 
> 'if no listitem has the value TR then we get nothing
> If li Is Nothing Then  
>     'set the first item as the selected item
>     dropdownlist1.SelectedIndex = 0
> Else
>     li.Selected = True
> End If
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Jim" wrote:
>
> > Let's say we have a drop down list (lstStates) of the 50 states. Is there
> > another way to pre select a state, when the page loads, besides
> > lstStates.SelectedValue = "NY" for example?
> >
> > The reason I'm asking is because when a value not in the 50 state master
> > list (for example, lstStates.SelectedValue = "TR"), the page erros out with a
> > "Specified argument was out of the range of valid values" error. I know the
> > correct answer is "well don't pass in TR" but that's another matter.
> >
> > Is there another property of the DropDownList where if it's not in the
> > master list, it simply goes to the first item in the list? Or is there a
> > way/trick to handle this so the page doesn't error out.
> >
> > Thanks in advance,
> > Jim