如何绑定另一个下拉列表... [英] How to bind one dropdown in another ...

查看:65
本文介绍了如何绑定另一个下拉列表...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是这个..在我的项目中我使用3下拉列表我想要,如果我选择第一个ddl然后在第二个ddl显示第一个相关数据然后我选择第二个显示第三个ddl数据..

但是我使用Below Code和2nd ddl正常工作,相同的代码用于第3,它们只显示第一个选择值。



我尝试过:



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();





}

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();


}

推荐答案

ASP:



ASP:

<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" />


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;



试试



{



con.Open();



ddlContinents.DataSource = cmd.ExecuteReader();



ddlContinents.DataTextField =ContinentName;



ddlContinents.DataValueField =ID;



ddlContinents.DataBind();



}



catch(例外情况)



{< br $> b $ b

抛出ex;



}



终于



{



con.Close();



con.Dispose();



}



}



}




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();

}

}

}


这篇关于如何绑定另一个下拉列表...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆