We have three tables 1)tblcontinents 2)tblCountry 3)tblState
Stored Procedure for the above named tables for doing operation in Cascading DropDownlist
create proc [dbo].[SP_GetContinent]
as
begin
select Continentid,Continentname from dbo.tblcontinents
end
create proc [dbo].[SP_GetCountriesByContinentID]
@Continent_id int
as
begin
select Countryid,Countryname from dbo.tblCountry where Continent_id =@Continent_id
end
create proc [dbo].[SP_GetStatesByCountry]
@Country_id int
as
begin
select Stateid,Statename from dbo.tblState where Country_id =@Country_id
end
Stored Procedure for the above named tables for doing operation in Cascading DropDownlist
create proc [dbo].[SP_GetContinent]
as
begin
select Continentid,Continentname from dbo.tblcontinents
end
create proc [dbo].[SP_GetCountriesByContinentID]
@Continent_id int
as
begin
select Countryid,Countryname from dbo.tblCountry where Continent_id =@Continent_id
end
create proc [dbo].[SP_GetStatesByCountry]
@Country_id int
as
begin
select Stateid,Statename from dbo.tblState where Country_id =@Country_id
end
We have to design as like below
<asp:DropDownList ID="DDL_Continent" runat="server" Width="150px" CausesValidation="true"
DataTextField="Continentname" DataValueField="Continentid" AutoPostBack="true"
onselectedindexchanged="DDL_Continent_SelectedIndexChanged" TabIndex="10">
</asp:DropDownList>
<asp:DropDownList ID="DDL_Country" runat="server" Width="150px"
DataTextField="Countryname" DataValueField="Countryid"
onselectedindexchanged="DDL_Country_SelectedIndexChanged" TabIndex="11" AutoPostBack="true">
</asp:DropDownList>
<asp:DropDownList ID="DDL_City" runat="server" Width="150px"
DataTextField="Statename" DataValueField="Stateid" TabIndex="12" AutoPostBack="true">
</asp:DropDownList>
This is reusable stored procedure function
private DataSet GetData(string spname, SqlParameter Parameter)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(spname, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
if (Parameter != null)
{
da.SelectCommand.Parameters.Add(Parameter);
}
ds = new DataSet();
da.Fill(ds);
return ds;
}
protected void DDL_Continent_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDL_Continent.SelectedIndex == 0)
{
DDL_Country.SelectedIndex = 0;
DDL_Country.Enabled = false;
DDL_City.SelectedIndex = 0;
DDL_City.Enabled = false;
}
else
{
DDL_Country.Enabled = true;
SqlParameter par = new SqlParameter("@Continent_id", DDL_Continent.SelectedValue);
ds = new DataSet();
ds = GetData("SP_GetCountriesByContinentID", par);
DDL_Country.DataSource = ds;
DDL_Country.DataBind();
ListItem li = new ListItem("Select Country", "-1");
DDL_Country.Items.Insert(0, li);
DDL_City.SelectedIndex = 0;
DDL_City.Enabled = false;
}
}
protected void DDL_Country_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDL_Country.SelectedIndex == 0)
{
DDL_City.SelectedIndex = 0;
DDL_City.Enabled = false;
}
else
{
DDL_City.Enabled = true;
SqlParameter par = new SqlParameter("@Country_id", DDL_Country.SelectedValue);
ds = new DataSet();
ds = GetData("SP_GetStatesByCountry", par);
DDL_City.DataSource = ds;
DDL_City.DataBind();
ListItem li = new ListItem("Select City", "-1");
DDL_City.Items.Insert(0, li);
DDL_City.SelectedIndex = 0;
DDL_City.Enabled = true;
}
}
We have to call this below method in Page_Load event
private void Initial_PageLoad()
{
DDL_Continent.DataSource = GetData("SP_GetContinent", null);
DDL_Continent.DataBind();
ListItem licont = new ListItem("Select Continent", "-1");
DDL_Continent.Items.Insert(0, licont);
ListItem liCountry = new ListItem("Select Country", "-1");
DDL_Country.Items.Insert(0, liCountry);
ListItem liCity = new ListItem("Select City", "-1");
DDL_City.Items.Insert(0, liCity);
}
No comments:
Post a Comment