Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ddl.SelectedIndex vs ddl.SelectedValue

Status
Not open for further replies.

ehx6

IS-IT--Management
May 20, 2004
150
0
0
US
ddl.SelectedIndex.ToString () // ?
ddl.SelectedValue // id usually int
ddl.SelectedItem.ToString() // actual value string

Hi , I am reading about drop down list, and was confused about ddl.SelectedIndex vs ddl.SelectedValue
I realize if I want to extract a column id I will use
ddl.SelectedValue
and if I want to extract a column value I will use
ddl.SelectedItem.ToString()

So, what is ddl.SelectedIndex.ToString () and how I can use it in my code thanks
ehx
 
Lets say you have a dropdownList with three items:
Code:
<asp:DropDownList id="ddl1" runat="server">
<asp:ListItem Value="1">Dog</asp:ListItem>
<asp:ListItem Value="2">Cat</asp:ListItem>
<asp:ListItem Value="3">Cow</asp:ListItem>
</asp:DropDownList>
The SelectedIndex is a zero based index of your ListItems. In this case Dog's SelectedIndex would be 0. Cat would be 1 and so on. One of the ways to use it in your code is to programmatically select a ListItem based on a condition or database value. Using the example above, if you put some code in your Page_Load like: ddl1.SelectedIndex = 2 then every time the page loaded Cow would be selected. If the first ListItem were a blank space instead of Dog you could determine that a user didn't make a selection by checking for a SelectedIndex of 0.
That's a pretty high level overview but hopefully enough to get you started. Try googling SelectedIndex for some real depth.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top