<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CasCading Dropdowns Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<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"></asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
private String strConnection = "Data Source=XZCBJ017550;Initial Catalog=MySamplesDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindContrydropdown();
}
}
/// <summary>
/// Bind COuntrydropdown
/// </summary>
protected void BindContrydropdown()
{
//conenction path for database
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from CountryTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryID";
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"));
}
/// <summary>
/// Bind State Dropdown Based on CountryID
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 StateTable where CountryID="+CountryID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlState.DataSource = ds;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateID";
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"));
}
}
/// <summary>
/// Bind Region dropdown based on Re
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 RegionTable where StateID=" + StateID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlRegion.DataSource = ds;
ddlRegion.DataTextField = "RegionName";
ddlRegion.DataValueField = "RegionID";
ddlRegion.DataBind();
ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
}
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
27 comments :
GREAT WORK.. WAS LOOKING FOR THIS FOR 2 DAYS
very well done boss...
It is very help full for me...
thanks.....
Please can u do exactly the same work without a postback to the web server.
hi , can u please provide the validations for the above code
I love you!!!!!!!!!!!!!!!!!!
nice
great work in simplified way
please suresh ji post with latest technology like mvc,linq,silverlight and more sharepoints
thanks for all posts
kishanthakur
this is best example i ever seen in any website , with such compact details.. i loved it..
thanks
Sir its Working, But when i click on "--submit--" its shows an exception "Input string was not in a correct format."
help me.
its displaying error like "both datasource and datasource id are same remove one thing".
very helpful.....................
plz help me out how to put a primary key and foreign to thse tables in the above example......
I have one requirement
Drop down list to choose types: NFC/RFID reader
when user choose reader show description in Label
Description for RFID Reader : Reading range 6-12 Meteres.
Description for NFC Reader : Reading Range 3-5 mm.
HELPED A LOT...MY HEARTFULL THANKS TO U SURESH BHAI..
sir ,
ist many many thanks to u.
this is very useful for my project
thanks sir
Really Enjoy ....Great Work!!
Sir pls post article for website live chat concept.
and also please give code to display the list of members in online for chat. really it will be helpful if you post this for us
changes are not reflecting in the second drop down
nice
suresh bhai ki jai..........
thanxs alot..
This coding is really useful for me and my friends
thanxs na
gud work dude...keep it up !!
It's very useful to me... Thankz boss
Hey Andhra Paris..
Love you so........much.
Hi, I am getting these errors (for every occurence of ddlCountry, ddlState, ddlRegion and
SqlConnection(strConnection))please help me to solve..
1)The name 'ddlCountry' does not exist in the current context
2)The name 'ddlState' does not exist in the current context
3)The name 'ddlRegion' does not exist in the current context
4)The best overloaded method match for 'System.Data.SqlClient.SqlConnection.SqlConnection(string)' has some invalid arguments
5)Argument 1: cannot convert from 'System.Data.SqlClient.SqlConnection' to 'string'