Cascading Drop Down List
Cascading DropDownList means a series of dependent DropDownLists where
one DropDownList is dependent on the parent or previous DropDownList and is
populated based on the item selected by the user. On many occasions we need to
make use of Cascading DropDownLists as I have here
Maximum Of Websites using Cascading DropDown List When Take Information From
User this is One of the Most Popular Task This Task Is Very Easy With Asp.net
Note:
When You Work With Cascading Drop Down List AutoPostBack Property Should be Enabled Because When select item in Parent DropDown list it will Post selected Item to Server and Get the data for Child DropDown List Depend on Selected Value
Properties of dropDownlist
DropDownList.SelectedIndex:-To get the Index value of selected item
DropDownList.SelectedItem:-Get The Selcted Item
DropDownList.SelectedValue:Get the Value of item
DropDownList.SelectedItem.Text:selected item Text
DropDownList.Enabled=true
DropDownList.DataTextField=Property For Displaying In Dropdown list
DropDownList.DataValueField=Value When You Select item value or Id of the Item
DropDownListState.Items.Insert(0, "-Select State-");
Program#
Sql Query For Create Country Table,State Table,City Table
--Cascading Drop Dow List
create Database MyTasks
use MyTasks
--Create Country Table
Create table Country(CountryId int Primary key,CountryName varchar(50) unique)
select*from Country
insert into Country values(1,'India')
insert into Country values(2,'Argentina')
insert into Country values(3,'China')
--Create State Table
Create table States(StateId int Primary key,StateName varchar(50) unique,CountryId int foreign key references Country(CountryId))
--Insert Values in State Table Statesin India
insert into States values(1,'Andhra Pradesh',1)
insert into States values(2,'Telangana',1)
insert into States values(3,'Tamil Nadu',1)
insert into States values(4,'Karnataka',1)
--states in argentina
insert into States values(5,'Argentina state1',2)
insert into States values(6,'Argentina state2',2)
--states in china
insert into States values(7,'Guangzhou',3)
insert into States values(8,'Shangai',3)
Create table Distric(DistricId int primary key,DistricName varchar(50) unique,StateId int foreign key references States(StateId))
insert into Distric values(1,'Guntur',1)
insert into Distric values(2,'Prakasam',1)
insert into Distric values(3,'Karnool',1)
insert into Distric values(4,'chitoor',1)
insert into Distric values(5,'nellore',1)
insert into Distric values(6,'anantapoor',1)
insert into Distric values(7,'kadapa',1)
insert into Distric values(8,'vizag',1)
insert into Distric values(9,'krishna',1)
insert into Distric values(10,'WestGodavari',1)
insert into Distric values(11,'vijayanagarm',1)
insert into Distric values(12,'srikakulam',1)
insert into Distric values(13,'Hyderabad',2)
insert into Distric values(14,'Nalgonda',2)
insert into Distric values(15,'rangareddy',2)
insert into Distric values(15,'waranagal',2)
insert into Distric values(16,'medak',2)
insert into Distric values(17,'mahaboobnagar',2)
insert into Distric values(18,'KarimNagar',2)
insert into Distric values(19,'NizamBad',2)
insert into Distric values(20,'Chennai',9)
insert into Distric values(21,'Trichy',9)
insert into Distric values(22,'Madhurai',9)
insert into Distric values(23,'Paundicherry',9)
--District in China States
insert into Distric values(24,'City1',5)
insert into Distric values(25,'City2',5)
insert into Distric values(26,'City3',6)
insert into Distric values(27,'City4',6)
insert into Distric values(28,'City5',7)
insert into Distric values(29,'City6',7)
insert into Distric values(30,'City7',8)
insert into Distric values(27,'City8',8)
DropDownList.aspx.cs Code
Drag and Drop 3 Drop DownList Controls
Enable Autopost Back Property to All the DropDownList
DropDownList.aspx.cs Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MultiTableEntity
{
public partial class CascadingDropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownListCountry.DataSource = CascadingDropDownList.Contryies();
DropDownListCountry.DataTextField = "CountryName";
DropDownListCountry.DataValueField = "CountryId";
DropDownListCountry.DataBind();
DropDownListCountry.Items.Insert(0, "-Select Country-");
DropDownListCity.Items.Insert(0, "-Select City-");
DropDownListState.Items.Insert(0, "-Select State-");
DropDownListState.Enabled = false;
DropDownListCity.Enabled = false;
}
}
//Get The Cities By State id
public static DataSet GetCity(int StateId)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
SqlCommand cmd = new SqlCommand("select*from Distric Where StateId='" + StateId + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
//Get States By Country Id
public static DataSet GetStates(int CountryId)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
SqlCommand cmd = new SqlCommand("select*from States Where CountryId='" +CountryId+ "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
//Get all Countryies
public static DataSet Contryies()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
SqlCommand cmd = new SqlCommand("select*from Country",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
//When Select Country Selected Here
protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListCountry.SelectedItem.Text == "-Select Country-")
{
DropDownListState.SelectedIndex = 0;
DropDownListCity.SelectedIndex = 0;
DropDownListState.Enabled = false;
DropDownListCity.Enabled = false;
}
else {
DropDownListState.Enabled = true;
int CountryId =int.Parse(DropDownListCountry.SelectedItem.Value);
DropDownListState.DataSource = CascadingDropDownList.GetStates(CountryId);
DropDownListState.DataTextField = "StateName";
DropDownListState.DataValueField = "StateId";
DropDownListState.DataBind();
DropDownListState.Items.Insert(0, "-Select State-");
}
}
//When Select State
protected void DropDownListState_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListState.SelectedItem.Text == "-Select State-")
{
DropDownListCity.SelectedIndex = 0;
DropDownListCity.Enabled = false;
}
else
{
DropDownListCity.Enabled = true;
int StateId = int.Parse(DropDownListState.SelectedItem.Value);
DropDownListCity.DataSource = CascadingDropDownList.GetCity(StateId);
DropDownListCity.DataTextField = "DistricName";
DropDownListCity.DataValueField = "DistricId";
DropDownListCity.DataBind();
DropDownListCity.Items.Insert(0, "-Select City-");
}
}
protected void DropDownListCity_SelectedIndexChanged(object sender, EventArgs e)
{
String City=DropDownListCity.SelectedItem.Text;
String State = DropDownListState.SelectedItem.Text;
String Country = DropDownListCountry.SelectedItem.Text;
Response.Write("You are Selected City:" + City + ",State:" + State + ",Country:" + Country);
}
}
}
No comments:
Post a Comment