In this post
I will explain Cascaded DropDown in Asp.Net.
Description:-
Cascaded
DropDown means when we select one dropdownlist then the second dropdownlist
will bind the data according to selection of first dropdownlist. In simple word
when we select country in one dropdownlist then in second dropdownlist city
will come according to country. It’s better to understand with pracrical
example so follow the code.
First make
the three table country state and city.
Country Table:-
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[country](
[country_id] [int] NOT NULL,
[country_name] [varchar](max) NOT NULL,
CONSTRAINT
[PK_country] PRIMARY KEY
CLUSTERED
(
[country_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
State Table :-
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[state](
[state_id] [int] NOT NULL,
[state_name] [varchar](max) NOT NULL,
[country_id] [int] NOT NULL,
CONSTRAINT
[PK_state] PRIMARY KEY
CLUSTERED
(
[state_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[state] WITH CHECK ADD CONSTRAINT
[FK_state_country] FOREIGN KEY([country_id])
REFERENCES [dbo].[country] ([country_id])
GO
ALTER TABLE [dbo].[state]
CHECK CONSTRAINT
[FK_state_country]
GO
City Tabel:-
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[city](
[city_id] [int] NOT NULL,
[city_name] [varchar](max) NOT NULL,
[state_id] [int] NOT NULL,
CONSTRAINT
[PK_city] PRIMARY KEY
CLUSTERED
(
[city_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[city] WITH CHECK ADD CONSTRAINT
[FK_city_city] FOREIGN KEY([state_id])
REFERENCES [dbo].[state] ([state_id])
GO
ALTER TABLE [dbo].[city]
CHECK CONSTRAINT
[FK_city_city]
GO
Run the
following code in your database.
Now create
web page.
DropDown.aspx:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<table
align="center">
<tr>
<td colspan="2" height="200px"
valign="bottom">
<h3>Cascading
Dropdowns Sample</h3>
</td>
</tr>
<tr>
<td>
Select Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry"
runat="server"
AutoPostBack="true"
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select State:
</td>
<td>
<asp:DropDownList ID="ddlState"
runat="server"
AutoPostBack="true"
onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select Region:
</td>
<td>
<asp:DropDownList ID="ddlRegion"
runat="server"
onselectedindexchanged="ddlRegion_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code behind page:-
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;
public partial class DropDown : System.Web.UI.Page
{
private String
strConnection = @"Data Source=SUSHIL-PC;Initial
Catalog=modi;Integrated Security=True";
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindContrydropdown();
}
}
protected void
BindContrydropdown()
{
//conenction path for database
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new
DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "country_name";
ddlCountry.DataValueField = "country_id";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--",
"0"));
ddlState.Items.Insert(0, new ListItem("--Select--",
"0"));
ddlRegion.Items.Insert(0, new ListItem("--Select--",
"0"));
}
protected void
ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from state where country_id="
+ CountryID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new
DataSet();
da.Fill(ds);
con.Close();
ddlState.DataSource = ds;
ddlState.DataTextField = "state_name";
ddlState.DataValueField = "state_id";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select--",
"0"));
if (ddlState.SelectedValue == "0")
{
ddlRegion.Items.Clear();
ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
protected void
ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(ddlState.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from city where state_id=" +
StateID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new
DataSet();
da.Fill(ds);
con.Close();
ddlRegion.DataSource = ds;
ddlRegion.DataTextField = "city_name";
ddlRegion.DataValueField = "city_id";
ddlRegion.DataBind();
ddlRegion.Items.Insert(0, new ListItem("--Select--",
"0"));
if (ddlState.SelectedValue == "0")
{
ddlRegion.Items.Clear();
ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
protected void
ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Debug and Run.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.