日期:2014-05-18  浏览次数:20511 次

两个dropdownlist关联选择选项
我现在有两个dropdownlist,第一个有两项:类型1/类型2
第二项根据第一项的选择绑定数据源,现在我第二项的数据源是数据库中两张不同的表,因为类型1和类型2的表结构不相同所以不能直接放在一张表里用select where提取,所以貌似第二个dropdownlist必须绑定不同的数据源,请问这个是必须在后台实现么?如何写?(我后台用的是C#)

------解决方案--------------------
描述模糊不清
在第一个dropdownlist添加autopostback=true属性及onselectedindexchanged事件,然后后台在该事件中获取drop1的选择项,然后动态给drop2绑定数据源即可
比如:
if(drop1.selectedvalue=="1")
 drop2.datasource=**.select**
else if (drop1.selectedvalue=="2")
drop2.datasource=**.select**
....
drop2.databind();
------解决方案--------------------

这个很好写啊。。。。

HTML code

//页面中放入两个DropDownList
   <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        
        <asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>

------解决方案--------------------
protected void ddlistName_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "请选择")
{
return;
}
string strValue = DropDownList1.SelectedValue
DataSet ds = 你方法(strValue);
ddlistInformation.DataSource = ds;
ddlistInformation.DataTextField = "绑定字段";
ddlistInformation.DataValueField = "绑定字段";
ddlistInformation.DataBind();
ddlistInformation.Items.Insert(0, new ListItem("请选择", "请选择"));
}
dropdownlist autopostback属性设为true
------解决方案--------------------
上面错了 是 DropDownList1_SelectedIndexChanged