我在DropDownList
内使用3 DataList
。因此,每行包含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
,我能够获得它的选择值,但我还需要尊重行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.
}
答案 0 :(得分:0)
我想你差不多了。你只需要。使用您的代码:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList1 = (DropDownList)sender;
var DropDownList2 = (DropDownList)sender;
var DropDownList3 = (DropDownList)sender;
string DDL1 = ((DropDownList)DropDownList1.NamingContainer.FindControl("DropDownList1")).SelectedValue;
string DDL2 = ((DropDownList)DropDownList2.NamingContainer.FindControl("DropDownList2")).SelectedValue;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
}
无论哪种方式,我都会这样做:
item valueddl1 = DropDownList1.SelectedValue;
等等;它应该像weel一样工作,而且更简单一点。