Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Problem is this.. in my project I use 3 dropdownlist and I want, If I select 1st ddl then in 2nd ddl show 1st related data and then I select 2nd show 3rd ddl data..
But I use Below Code and 2nd ddl working properly and same code is used for 3rd the they show only first select value.

What I have tried:

private void ddBindCategory()
{

ddL1.DataSource = ProductBAL.Instance.GetCatfirst();
ddL1.DataTextField = "NameL1";
ddL1.DataValueField = "IDL1";
ddL1.DataBind();
ddBindsub();

}
public void ddBindsub()
{
DataTable dt = new DataTable();
ddL2.DataSource = ProductBAL.Instance.GetCatSecond(ddL1.SelectedValue);
ddL2.DataTextField = "NameL2";
ddL2.DataValueField = "IDL2";
ddL2.DataBind();
ddBindthird();
}

public void ddBindthird()

{
ddL3.DataSource = ProductBAL.Instance.GetCatThird(ddL2.SelectedValue);
ddL3.DataTextField = "NameL3";
ddL3.DataValueField = "IDL3";
ddL3.DataBind();



}


protected void ddL1_SelectedIndexChanged(object sender, EventArgs e)
{


ddL2.DataSource = ProductBAL.Instance.GetCatSecond(ddL1.SelectedValue);
ddL2.DataTextField = "NameL2";
ddL2.DataValueField = "IDL2";
ddL2.DataBind();

}


protected void ddL2_SelectedIndexChanged1(object sender, EventArgs e)
{
ddL3.DataSource = ProductBAL.Instance.GetCatThird(ddL2.SelectedValue);
ddL3.DataTextField = "NameL3";
ddL3.DataValueField = "IDL3";
ddL3.DataBind();


}
Posted
Updated 28-Jun-16 21:26pm

ASP:

ASP.NET
<span style ="font-family:Arial">Select Continent : </span>

<asp:DropDownList ID="ddlContinents" runat="server" AutoPostBack = "true"

             OnSelectedIndexChanged="ddlContinents_SelectedIndexChanged">

<asp:ListItem Text = "--Select Continent--" Value = ""></asp:ListItem>

</asp:DropDownList>

 

<br /><br />

<span style ="font-family:Arial">Select Country : </span>

<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack = "true"

Enabled = "false"  OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">

<asp:ListItem Text = "--Select Country--" Value = ""></asp:ListItem>

</asp:DropDownList>

 

<br /><br />

<span style ="font-family:Arial">Select City : </span>

<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack = "true"

 Enabled = "false" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">

<asp:ListItem Text = "--Select City--" Value = ""></asp:ListItem>

</asp:DropDownList>

 

<br /><br />

<asp:Label ID="lblResults" runat="server" Text="" Font-Names = "Arial" />
 
Share this answer
 
C#
C#



protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

ddlContinents.AppendDataBoundItems = true;

String strConnString = ConfigurationManager

.ConnectionStrings["conString"].ConnectionString;

String strQuery = "select ID, ContinentName from Continents";

SqlConnection con = new SqlConnection(strConnString);

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = strQuery;

cmd.Connection = con;

try

{

con.Open();

ddlContinents.DataSource = cmd.ExecuteReader();

ddlContinents.DataTextField = "ContinentName";

ddlContinents.DataValueField = "ID";

ddlContinents.DataBind();

}

catch (Exception ex)

{

throw ex;

}

finally

{

con.Close();

con.Dispose();

}

}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900