|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DropDownListLet'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 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 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
Finding a way to bind ASP.NET controls(two way) to a typed-dataset
Error in DataList test query - aspnet 2.0 Gridview Updates not writing to database after clicking update Datagrid paging I lose my sort Postback in Web Controls client-side .NET control Dynamically Adding DataLists Coloring Calendar !!! Combining 2 Fields Using ListBox.DataTextField Status text |
|||||||||||||||||||||||