【发布时间】:2015-01-21 11:27:34
【问题描述】:
我在DataList 中使用了 3 个DropDownList。因此,每一行包含 3 个DropDownList。 DataList 还包含 DataKeyField。
现在,如果用户从DropDownList1 中选择值,那么我想绑定DropDownList2,如果用户从DropDownList2 中选择值,那么我想绑定DropDownList3。我正在使用SelectedIndexChanged,并且能够获得相关DropDownList 选定值的值。但是,如果用户选择DropDownList2,那么我还需要DropDownList1 的值,还需要尊重DataKeyField 的值。
这个怎么弄???
我的代码示例:
<asp:DataList ID="dlTest" runat="server" OnItemDataBound="dlTest_ItemDataBound"
DataKeyField="TestId">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged"> </asp:DropDownList>
</ItemTemplate>
</asp:DataList>
在这里,onSelectedIndexChanged 的 DropDownList3,我能够获得它的 selectedValue,但我还需要尊重行的 selectedValue DropDownList1 和 DropDownList2 以及尊重 DataKeyField 值。
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList3 = (DropDownList)sender;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
// Also need selectedValue of DropDownList1 and DropDownList2 and DataKeyField.
}
【问题讨论】:
-
如果我没记错的话,代码就在那里。而不是
.FindControl("DropDownList3"),只需对“DropDownList1”和“DropDownList2”进行查找控制。
标签: c# asp.net drop-down-menu datalist