Tuesday 1 April 2014

How to implement cascading dropdown list in asp.net (Or) how to populate one dropdown based on selection in another dropdown in ASP.NET.

In this article I will explain how to populate one dropdownlist based on selection in another dropdown in ASP.NET.

Or

How to implement cascading dropdown list in asp.net

I have three dropdowns Country dropdown lists, State dropdown, City dropdown I need to populate States dropdownlist based on Country dropdownlist and I need to populate City dropdown based on States dropdown.

I have three tables in DataBase.

1.      Country Table (tblCountry)

2.      State Table (tblState)

3.      City Table (tblCity)



First we need to establish a connection in Web.config file.

<connectionStrings>
<add name="connectionString" connectionString="Data Source=LocalHost;uid=sa1;password=Con@123;Initial Catalog=practice"/>
</connectionStrings>

Below is the page design.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Populate one dropdown based on selection in another dropdown</title>
</head>
<body>
<form id="form1" runat="server">
<table align="center">
<tr>
<td colspan="2" style="color: maroon; font-size:large;">
<b><u>Populate one dropdown based on selection in another dropdown</u></b>
</td>
</tr>
<tr>
<td colspan="2" height="20px"></td>
</tr>
<tr>
<td>Select Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" Width="150px"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Select State:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" Width="150px"
OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Select City:
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" Width="150px"></asp:DropDownList>
</td>
</tr>
</table>
</form>
</body>
</html>

Below is the total code for Dropdowns lists cascading code.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class DropDownlistSelectedChanged : System.Web.UI.Page
{
SqlConnection conCascading = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountrydropdown();
}
}
protected void BindCountrydropdown()
{
conCascading.Open();
SqlCommand cmdCountry = new SqlCommand("select * from tblCountry", conCascading);
SqlDataAdapter daCountry = new SqlDataAdapter(cmdCountry);
DataSet dsCountry = new DataSet();
daCountry.Fill(dsCountry);
conCascading.Close();
ddlCountry.DataSource = dsCountry;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryID";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select Country--", "0"));
ddlState.Items.Insert(0, new ListItem("--Select State--", "0"));
ddlCity.Items.Insert(0, new ListItem("--Select City--", "0"));

}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
conCascading.Open();
SqlCommand cmdstate = new SqlCommand("select * from tblState where CountryID=" + Convert.ToInt32(ddlCountry.SelectedValue), conCascading);

cmdstate.ExecuteNonQuery();
SqlDataAdapter dastate = new SqlDataAdapter(cmdstate);
DataSet dsstate = new DataSet();
dastate.Fill(dsstate);
conCascading.Close();

ddlState.DataSource = dsstate;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateID";
ddlState.DataBind();

ddlState.Items.Insert(0, new ListItem("--Select State--", "0"));
ddlCity.Items.Insert(0, new ListItem("--Select City--", "0"));
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
conCascading.Open();
SqlCommand cmdCity = new SqlCommand("select * from tblCity where StateID=" + Convert.ToInt32(ddlState.SelectedValue), conCascading);

cmdCity.ExecuteNonQuery();
SqlDataAdapter daCity = new SqlDataAdapter(cmdCity);
DataSet dsCity = new DataSet();
daCity.Fill(dsCity);
conCascading.Close();

ddlCity.DataSource = dsCity;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem("--Select City--", "0"));
}
}

Output:






1 comment:

  1. Hi sathya this is vijay same example i want to implement in 3tier architecture please send me implementation code as soon as possible

    ReplyDelete