Tuesday, April 11, 2017

Retrieving Selected Item Text, Value and Index of Dropdown

Today we will check in Asp.Net, how to get a value from dropdown, its item text and index also. so i think without discussing more on theory let's go to code and create a dropdown with some sample data.

<asp:DropDownList ID="ddlSampleData" runat="server">
    <asp:ListItem Text="Select Name" Value="-1"></asp:ListItem>
    <asp:ListItem Text="Pakistan" Value="1"></asp:ListItem>
    <asp:ListItem Text="England" Value="2"></asp:ListItem>
    <asp:ListItem Text="Ireland" Value="3"></asp:ListItem>
</asp:DropDownList>

<asp:Button ID="btnGetValddl" runat="server" Text="Button" onclick="btnGetValddl_Click" /> 

You can create a dropdown by using Asp.net toolbox, just drag and drop the dropdown control after that add some listitem to dropdown from proprty or by code. We created a asp button also to perform the operations.
On this button click event we will get the value, text and index of selected item with a very simple way and then we will write this to our page by using HttpResponse's Write method. here is the code for that button click event.
protected void btnGetValddl_Click(object sender, EventArgs e)
    {
        if (ddlSampleData.SelectedValue != "-1")
        {
            Response.Write("Selected Item's Text = " + ddlSampleData.SelectedItem.Text + "<br/>");
            Response.Write("Selected Item's Value = " + ddlSampleData.SelectedItem.Value + "<br/>");
            Response.Write("Selected Item's Index = " + ddlSampleData.SelectedIndex.ToString());
        }
        else
        {
            Response.Write("Select Countrt First!");
        }
    }


As on the first item of dropdown we set the value as -1, this value will tell us either a country is selected from the dropdown or not and if no country is selected then we will show message "select country first".
To get the select item Text we used:
ddlSampleData.SelectedItem.Text

To get the Select Item's Value we can:
ddlSampleData.SelectedItem.Value

or
ddlSampleData.SelectedValue
To get the index of Select Item's, we can do:
ddlSampleData.SelectedIndex

we can perform many other operation like if we want to set a value by default it should be select like if we want in our dropdown pakistan should come selected by default then we can set on page load event like:
 ddlSampleData.SelectedValue = "1";

or we can achieve this by using selectedIndex:
 ddlSampleData.SelectedIndex = 1;

if we want to bind our dropwdown directly data from data then we can do something like below, for example we have to bind a city dropdown:
<asp:DropDownList ID="slctCity" runat="server"></asp:DropDownList>


we are going to create a function so we can call this from different pages:
public void ddlbind(DropDownList ddl, string query, string text, string val)
    {
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(query, con);
        da.Fill(ds);
        ddl.DataSource = ds.Tables[0];
        ddl.DataTextField = text;
        ddl.DataValueField = val;
        ddl.DataBind();
    }

in this you need to set con with your SQL Connection and after that pass just query and dropdown id.
we will call like this:
ddlbind(slctCity, "select ct_name,ct_charges from tbl_city", "ct_name", "ct_charges"); 

Comments and Suggestions are Always Welcome!

No comments:

Post a Comment