Dropdownlist/SQL question

Jackpanel

Freshman
Joined
Feb 23, 2004
Messages
35
I've got a dropdown list that I've databound to a table in my database. What I need to do now is select the proper list item according to another query.

Code:
<asp:DropDownList class="inputbox" id="ddStatus"
runat="server" DataValueField="ID" DataTextField="StatusType" >
</asp:DropDownList>

The resulting contents of the databound dropdown could look like this:

Code:
<select name="ddStatus" id="ddStatus" class="inputbox">
	<option value="237">Choice 1</option>
	<option value="584">Choice 2</option>
	<option value="192">Choice 3</option>
	<option value="871">Choice 4</option>
</select>

Now say I query the database, and want the selected item in this dropdownlist to be the one with the value of 584. I need to know what the index number is of the option with this value, i.e. ddStatus.SelectedIndex = 2. How would I go about doing this? Is there a way to loop through the dropdownlist I just created and look for the option with a value matching 584?
 
Code:
ddStatus.Items.FindByValue("584").Selected = True

or preferably

Code:
ddStatus.SelectedIndex = ddStatus.Items.IndexOf(ddStatus.Items.FindByValue("584"))
 
Back
Top