Retrieving selected item text, value and index of an asp.net dropdownlist - Part 21

Suggested Videos
Part 16 - Dropdownlist in asp.net
Part 17 - Data bind dropdownlist with data from the database
Part 18 - Data bind dropdownlist with data from an xml file

In this video we will discuss about retrieving the selected item text, index and value from a dropdownlist. Let's first add some items to the dropdownlist. To do this
1. Drag and drop a dropdownlist and a button control onto the webform
2. Copy and paste the following HTML on the webform.
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="Select Continent" Value="-1"></asp:ListItem>
    <asp:ListItem Text="Asia" Value="1"></asp:ListItem>
    <asp:ListItem Text="Europe" Value="2"></asp:ListItem>
    <asp:ListItem Text="Africa" Value="3"></asp:ListItem>
    <asp:ListItem Text="North America" Value="4"></asp:ListItem>
    <asp:ListItem Text="South America" Value="5"></asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />



Copy and paste the following code in the code behind file.
protected void Button1_Click(object sender, EventArgs e)
{
    if (DropDownList1.SelectedValue != "-1")
    {
        Response.Write("Selected Item Text = " + DropDownList1.SelectedItem.Text + "<br/>");
        Response.Write("Selected Item Value = " + DropDownList1.SelectedItem.Value + "<br/>");
        Response.Write("Selected Item Index = " + DropDownList1.SelectedIndex.ToString());
    }
    else
    {
        Response.Write("Please select the continent");
    }
}



Explanation of the code:
1. In the button click event, we first check if the user has made a selection. If the DropDownList1.SelectedValue is -1, then we know that user has not made a choice.
if (DropDownList1.SelectedValue != "-1")
2. If the user has made a selection in the DropDownList, then we retrieve the Text of the Selected ListItem object as shown below.
DropDownList1.SelectedItem.Text
3. To retrieve the Selected item value from the DropDownList, we can use
DropDownList1.SelectedItem.Value 
OR
DropDownList1.SelectedValue
4. To get the index of the Selected ListItem object of the dropdownlist, we use SelectedIndex property as shown below.
DropDownList1.SelectedIndex

The SelectedIndex and SelectedValue properties of the DropDownList can also be used to have a list item selected in the DropDownList. For example, the following code would default the selection to Asia when the page loads for the first time.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList1.SelectedValue = "1";
    }
}

The same thing can also be achieved using the SelectedIndex property as shown below.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList1.SelectedIndex = 1;
    }
}

2 comments:

  1. instead of 1 ( DropDownList1.SelectedValue = "1";) can we give text which is in DB(table), so as to set the DropDownList1 to a selected state, as per the value in the row

    ReplyDelete

It would be great if you can help share these free resources