根据DataList中的另一个Dropdown SelectedValue绑定一个下拉列表 - c#

时间:2015-01-21 11:27:35

标签: c# asp.net drop-down-menu datalist

我在DropDownList内使用3 DataList。因此,每行包含3 DropDownListDataList还包含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,我能够获得它的选择值,但我还需要尊重行DropDownList1DropDownList2的所选值,以及尊重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.
    }

1 个答案:

答案 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一样工作,而且更简单一点。

相关问题