Gautam Buddha Temple Varanasi,Varanasi - Buddhist Pilgrimage

Monday, August 23, 2010

Cascading Dropdownlist

ASPX Page

<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True" DataTextField="CountryName"
DataValueField="CountryId" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged";>
<asp:ListItem Value="0" Text="<Select Country;>";></asp:ListItem;>
</asp:DropDownList;>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" DataTextField="StateName"
DataValueField="StateId" OnSelectedIndexChanged="ddlState_SelectedIndexChanged";>
</asp:DropDownList;>
<asp:DropDownList ID="ddlCity" runat="server" DataTextField="CityName" DataValueField="CityId";>
</asp:DropDownList;>

CS Page

if (!Page.IsPostBack)
{
CountryBind();
}
protected void CountryBind()
{
string sqlQuery = "Select * from Country;

using (SqlConnection conn = new SqlConnection(conString))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
da.Fill(ds);
ddlCountry.DataSource = ds;
ddlCountry.DataBind();
conn.Close();
}
}
protected void StateBind()
{
string sqlQuery = "SELECT * FROM State " + " WHERE (CountryId = " + Convert.ToInt32(ddlCountry.SelectedValue) + " )";
using (SqlConnection conn = new SqlConnection(conString))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
da.Fill(ds);
ddlState.DataSource = ds;
ddlState.DataBind();
conn.Close();
}
}
protected void CityBind()
{
string sqlQuery = "SELECT * FROM City " + " WHERE (StateId = " + Convert.ToInt32(ddlState.SelectedValue) + " )";
using (SqlConnection conn = new SqlConnection(conString))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
da.Fill(ds);
ddlCity.DataSource = ds;
ddlCity.DataBind();
conn.Close();
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
StateBind();
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
CityBind();
}

Thanks & Regards
Santosh Singh

No comments:

Post a Comment