Here I am going to explain how to Bind State ,Districts and City in Dropdownlist using c#.net.
Here I am using AJAX Update panel to perform Client-site Operation.
Step 1 :
Create 3 tables Namely State, District and Cities.
Like the Below Image. Table Name : State
Here I am using AJAX Update panel to perform Client-site Operation.
Step 1 :
Create 3 tables Namely State, District and Cities.
Like the Below Image. Table Name : State
Table Name : District
Table Name : Cities
Output :
Step :2
Now Copy and Paste the Below Code in .aspx page :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CasCading Like DropDownList</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="style1">
<tr><td>
<td> </td></td>
</tr>
<tr>
<td class="style2">State Name :</td><td><asp:DropDownList
ID="ddlState" runat="server" AutoPostBack = "true"
OnSelectedIndexChanged = "State_Changed" Height="21px"
Width="130px"></asp:DropDownList>
</td><td> </td></tr><tr><td class="style2">District Name :</td><td>
<asp:DropDownList
ID="ddlDistrict" runat="server"
AutoPostBack = "true" OnSelectedIndexChanged = "District_Changed"
Height="18px" Width="130px"></asp:DropDownList>
</td><td> </td></tr><tr><td class="style2">City Name :</td><td><asp:DropDownList
ID="ddlCities" runat="server" Height="22px" Width="130px"></asp:DropDownList>
</td><td> </td></tr>
<tr><td>
<td> </td></td>
</tr></table>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Step : 3
Declare the Connection In Web.config file
<connectionStrings>
<add name="ConString" connectionString="Data Source=saroj;Initial Catalog=CascadingDB;Integrated Security=True"/>
</connectionStrings>
Step :4
Now Go to Code-behind page (.aspx.cs)
And Declare NameSpace
using System.Data.SqlClient;
using System.Configuration;
Copy and Paste the code in .cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select StateId, StateName from State";
BindDropDownList(ddlState, query, "StateName", "StateId", "Select State");
ddlDistrict.Enabled = false;
ddlCities.Enabled = false;
ddlDistrict.Items.Insert(0, new ListItem("Select District", "0"));
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
}
}
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}
protected void State_Changed(object sender, EventArgs e)
{
ddlDistrict.Enabled = false;
ddlCities.Enabled = false;
ddlDistrict.Items.Clear();
ddlCities.Items.Clear();
ddlDistrict.Items.Insert(0, new ListItem("Select District", "0"));
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
int StateId = int.Parse(ddlState.SelectedItem.Value);
if (StateId > 0)
{
string query = string.Format("select DistrictId, DistrictName from District where StateId = {0}", StateId);
BindDropDownList(ddlDistrict, query, "DistrictName", "DistrictId", "Select District");
ddlDistrict.Enabled = true;
}
}
protected void District_Changed(object sender, EventArgs e)
{
ddlCities.Enabled = false;
ddlCities.Items.Clear();
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
int DistrictId = int.Parse(ddlDistrict.SelectedItem.Value);
if (DistrictId > 0)
{
string query = string.Format("select CityId, CityName from Cities where DistrictId = {0}", DistrictId);
BindDropDownList(ddlCities, query, "CityName", "CityId", "Select City");
ddlCities.Enabled = true;
}
}
No comments:
Post a Comment