日期:2014-05-19  浏览次数:20527 次

asp.net中两个dropdownlist如何实现联动
A表字段为   Class_Id   Location_Id  
B表字段为   Locaton_Id  
B表的Location_Id对应A表中相同Location_Id的多条记录  

在page_Load中已经将表A的字段Class_Id绑定到dropdownlistA中,表B的字段location_Id绑定到dropdownlistB中  
我想实现的功能是在表单中,当选择dropdownlistB的Location_Id时,在dropdownlistA中会筛选出Location_Id相同的Class_Id  
请问在asp.net中如何实现(不用sqldatasource组件实现,是用代码编写完成的,我用的是asp.net+sql   server)


------解决方案--------------------
说错了,是AutoPostBack改为True,这样每选一个,都会提交,然后对ddlA进行筛选,重新绑定就行了
------解决方案--------------------
<asp:DropDownList ID= "dropdownlistA " runat= "server " OnSelectedIndexChanged= "dropdownlistA_SelectedIndexChanged "
AutoPostBack= "true ">
</asp:DropDownList> &nbsp; <asp:DropDownList ID= "dropdownlistB " runat= "server ">
</asp:DropDownList>


DataTable dtdropdownlistA= DALdropdownlistA.C_Select(int.Parse(dropdownlistA.SelectedValue));
dropdownlistB.Items.Add(new ListItem( "—请选择— ", "0 "));
for (int j = 1; j < dtdropdownlistA.Rows.Count + 1; j++)
{
dropdownlistB.Items.Add(new ListItem(dtdropdownlistA.Rows[j - 1][ "Class_Id "].ToString(), dtMiddleBusiness.Rows[j - 1][ "Class_Id "].ToString()));
}
dropdownlistB.DataBind();
------解决方案--------------------
不采用无刷新联动的话很简单,将第一个dropdowlist的值做为查询的对象,去查询另个要查的表,然后绑定第二个dropdownlist,AutoPostBack要设置为true