My source code:
<div>
<table>
<tr><td><asp:DropDownList ID="ddl_Continent" runat="server" AutoPostBack="true"
onselectedindexchanged="ddl_Continent_SelectedIndexChanged"><asp:ListItem Text="--Select Continent--" Value=""></asp:ListItem></asp:DropDownList></td></tr>\
<tr><td></td></tr>
<tr><td><asp:DropDownList ID="ddl_Country" Enabled="false" runat="server"
AutoPostBack="true" onselectedindexchanged="ddl_Country_SelectedIndexChanged"><asp:ListItem Text="--Select Country--" Value=""></asp:ListItem></asp:DropDownList></td></tr>
<tr><td></td></tr>
<tr><td><asp:DropDownList ID="ddl_City" Enabled="false" runat="server"
AutoPostBack="true" onselectedindexchanged="ddl_City_SelectedIndexChanged"><asp:ListItem Text="--Select City--" Value=""></asp:ListItem></asp:DropDownList></td></tr>
<tr><td></td></tr>
</table>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>
---------------------------
.aspx.cs code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddl_Continent.AppendDataBoundItems = true;
string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strSelect = "select ID,ContinentName from Continent";
SqlConnection cn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSelect;
cmd.Connection = cn;
try
{
cn.Open();
ddl_Continent.DataSource = cmd.ExecuteReader();
ddl_Continent.DataTextField = "ContinentName";
ddl_Continent.DataValueField = "ID";
ddl_Continent.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cn.Close();
cn.Dispose();
}
}
}
protected void ddl_Continent_SelectedIndexChanged(object sender, EventArgs e)
{
ddl_Country.Items.Clear();
ddl_Country.Items.Add(new ListItem("--Select Country--:", ""));
ddl_City.Items.Clear();
ddl_City.Items.Add(new ListItem("--Select City--:", ""));
ddl_Country.AppendDataBoundItems = true;
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strQuery = "select ID,ContinentID,CountryName from Country" + " where ContinentID=@ContinentID";
SqlConnection con = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("ContinentID", ddl_Continent.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddl_Country.DataSource=cmd.ExecuteReader();
ddl_Country.DataTextField="CountryName";
ddl_Country.DataValueField="ID";
ddl_Country.DataBind();
if(ddl_Country.Items.Count>1)
{
ddl_Country.Enabled=true;
}
else
{
ddl_Country.Enabled=false;
ddl_City.Enabled=false;
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected void ddl_Country_SelectedIndexChanged(object sender, EventArgs e)
{
ddl_City.Items.Clear();
ddl_City.Items.Add(new ListItem("--Select city--", "1"));
string strcon1 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
ddl_City.AppendDataBoundItems = true;
string strQry = "select ID,CityName from City" + "where CountryID=@CountryID";
SqlConnection con1 = new SqlConnection(strcon1);
SqlCommand cmd = new SqlCommand();
//cmd.Parameters.Add("@CountryID", SqlDbType.VarChar).Value = ddl_Country.SelectedValue.ToString();
cmd.Parameters.AddWithValue("@CountryID", ddl_Country.SelectedValue);
//if (ddl_Country.SelectedItem.Value != "" && ddl_Country.SelectedItem.Value != string.Empty)
//{
// cmd.Parameters.AddWithValue("@CountryID", ddl_Country.SelectedItem.Value);
//}
//else
//{
// cmd.Parameters.AddWithValue("@CountryID", DBNull.Value);
//}
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQry;
cmd.Connection = con1;
try
{
con1.Open();
ddl_City.DataSource = cmd.ExecuteReader();
ddl_City.DataTextField = "CityName";
ddl_City.DataValueField = "ID";
//ddl_Country.Items.Add(new ListItem("--Select Country--", "1"));
ddl_City.DataBind();
if (ddl_City.Items.Count > 1)
{
ddl_City.Enabled = true;
}
else
{
ddl_City.Enabled = false;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con1.Close();
con1.Dispose();
}
}
protected void ddl_City_SelectedIndexChanged(object sender, EventArgs e)
{
lblMessage.Text = "You Selected" + ddl_Continent.SelectedItem.Text + "---->" + ddl_Country.SelectedItem.Text + "---->" + ddl_City.SelectedItem.Text;
}
}
---------------------------------------------------------------
ddl_Continent_SelectedIndexChanged --it's working 5ne.
only problem in ddl_Country_SelectedIndexChanged event.
once check my code.
maninaanee-8287, if this helps please login to Mark As Answer. | Alert Moderator