articles

Home / DeveloperSection / Articles / Working with DropDownList Controls in ASP.NET

Working with DropDownList Controls in ASP.NET

AVADHESH PATEL 5417 19-Sep-2012

Use the DropDownList control to create a single selection drop-down list control. The DropDownList control also supports data binding. To bind the control to a data source, first create a data source, such as a System.Collections.ArrayList, that contains the items to display in the control. Next, use the Control.DataBind method to bind the data source to the DropDownList control. Use the DataTextField and DataValueField properties to specify which field in the data source to bind to the Text and Value properties of each list item in the control, respectively. The DropDownList control will now display the information from the data source.

Use the SelectedIndex property to programmatically determine the index of the item selected by the user from the DropDownList control. The index can then be used to retrieve the selected item from the Items collection of the control.

In this article a have created a demo for selecting Country, State and City. DropDownList’s items populated according selected DropDownList item.

.aspx code 
<table class="style1">
        <tr>
            <td>
                Country</td>
            <td>
                <asp:DropDownList Width="130px" ID="ddlCountry" runat="server"
                    AutoPostBack="True" onselectedindexchanged="ddlCountry_SelectedIndexChanged">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                State</td>
            <td>                 <asp:DropDownList Width="130px" ID="ddlState" runat="server"
                AutoPostBack="true" onselectedindexchanged="ddlState_SelectedIndexChanged">
                </asp:DropDownList></td>
        </tr>
        <tr>
            <td>
                City</td>
            <td>
                <asp:DropDownList ID="ddlCity" Width="130px" runat="server">
                </asp:DropDownList></td>
        </tr>
    </table>

  .cs code 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
 
public partial class DropDownList : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter adap;
    DataTable dtCountry;
    DataTable dtState;
    DataTable dtCity; 
    string conString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString; 
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(conString); 
        if (!IsPostBack)
        {
            ddlCountry.DataSource = fillCountry();
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "pkCountryId";
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem("Select", "0"));
        }
    }
 
    public DataTable fillCountry()
    {
        adap = new SqlDataAdapter("select * from Country", con);
        dtCountry = new DataTable();
        adap.Fill(dtCountry);
        return dtCountry;
    }
 
    public DataTable fillState(int countryid)
    {
        adap = new SqlDataAdapter("select * from State where fkCountryId=" + countryid + "", con);
        dtState = new DataTable();
        adap.Fill(dtState);
        return dtState;
    } 
    public DataTable fillCity(int stateid)
    {
        adap = new SqlDataAdapter("select * from City where fkStateId="+ stateid +"",con);
        dtCity = new DataTable();
        adap.Fill(dtCity);
        return dtCity;
    }
 
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlState.DataSource = fillState(Convert.ToInt32(ddlCountry.Text));         ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "pkStateId";
        ddlState.DataBind();
        ddlState.Items.Insert(0, new ListItem("Select", "0"));
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlCity.DataSource = fillCity(Convert.ToInt32(ddlState.Text));
        ddlCity.DataTextField = "CityName";
        ddlCity.DataValueField = "pkCityId";
        ddlCity.DataBind();
        ddlCity.Items.Insert(0,new ListItem("Select","0"));
    }
}

 

 


Updated 29-Nov-2017
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By