Tuesday, September 22, 2015

Retrieving selected item text value and index of dropdownlist

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;
    }
}


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Related Posts:

  • What is ASP.NET ASP.NET is a Web application framework developed by Microsoft to build dynamic data driven Web applications and Web services.  1. ASP.NET is a … Read More
  • OLEDB Connection string The C# OleDbConnection instance takes Connection String as argument and pass the value to the Constructor statement. An instanc… Read More
  • Events in the life cycle of a web application In a web application, events can occur at 3 levels 1. At the Application Level(Example: Application Start) 2. At the Page Level(Example: Page Load) … Read More
  • What is viewstate In this video session, we will learn about 1. Stateless nature of HTTP protocol 2. How a webform is processed 3. What is ViewState 4. The role of Vi… Read More
  • Creating your first ASP.NET website In this video session 1. We will learn about using visual studio 2. Creating your first ASP.NET web application 3. Learn about different windows in … Read More

0 comments:

Post a Comment