How To Create Cascading Drop downlist in ASP.NET
">How To Create Cascading Drop downlist in ASP.NET
-----------------------------------------------------------------------------------------------------------------------------------------------
//Create Table Country
-----------------------------------------------------------------------------------------------------------------------------------------------
Create TABLE COUNTRY
(
COUNTRY_ID int Primary key,
COUNTRY_NAME nvarchar(20)not null
)
-------------------------------------------------------------------------------------------------------------------------------------------------
//Create Table STATE and Use Primary Key from Country Table as Foreign Key
-------------------------------------------------------------------------------------------------------------------------------------------------
create table STATE1
(
STATEID int Primary Key,
COUNTRY_ID int,
STATENAME nvarchar(20)not null
FOREIGN KEY (COUNTRY_ID) REFERENCES COUNTRY(COUNTRY_ID)
)
Asp.Net SourceCode:
------------------------------------------------------------------------------------------------------------------------------------------------
//Insert Two DrropDownList on your Page first DrropDownList Name ddlCountry and 2nd DrropDownList Name ddState and write this down here Code on Page_load
---------------------------------------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="D.aspx.cs" Inherits="D" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Country:<asp:DropDownList ID="ddlCountry" runat="server" Height="14px"
Width="150px">
</asp:DropDownList>
<br />
STATE:<asp:DropDownList ID="ddlState" runat="server" Height="14px"
Width="150px">
</asp:DropDownList>
<br />
</div>
</form>
</body>
</html>
CODE C#
-----------------------------------------------------------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlCommand cmd = new SqlCommand("Select * from COUNTRY", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCountry.DataSource = ds;
ddlCountry.DataValueField = "COUNTRY_ID";
ddlCountry.DataTextField = "COUNTRY_NAME";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("----Select Country----"));
}
------------------------------------------------------------------------------------------------------------------------------------------------
//Write this Code Dabble Click on Country DroppDownList
------------------------------------------------------------------------------------------------------------------------------------------------
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int COUNTRY_ID= Convert.ToInt32(ddlCountry.SelectedValue);
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlCommand cmd=new SqlCommand("Select * from CITY Where COUNTRY_ID='"+COUNTRY_ID+"'",cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlState.DataSource = ds;
ddlState.DataValueField = "STATE_ID"; //
ddlState.DataTextField = "STATE_NAME";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("----Select State----"));
}