2015-01-21 59 views
0

我在DataList內部使用3 DropDownList。所以,每行包含3個DropDownListDataList也包含DataKeyField

現在,如果用戶從DropDownList1選擇值,那麼我想綁定DropDownList2,如果用戶從DropDownList2選擇值,那麼我想要綁定DropDownList3。我正在使用SelectedIndexChanged,我可以獲得相關的DropDownList選定值的值。但是,如果用戶選擇DropDownList2那麼我也需要DropDownList1的值,並且還需要值得尊重的DataKeyField

如何得到這個???

我的代碼示例:基於DataList中另一個Dropdown SelectedValue綁定一個下拉菜單 - c#

<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> 

這裏,DropDownList3onSelectedIndexChanged,我能得到它了selectedValue,但我還需要DropDownList1DropDownList2推崇行的了selectedValue,也尊重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

如果我沒有弄錯,你幾乎已經有了代碼。而不是'.FindControl(「DropDownList3」)',只需對「DropDownList1」和「DropDownList2」進行查找控制。 – 2015-01-21 14:05:03

回答

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一樣工作,而且更簡單一些。

+0

爲同一個對象創建三個變量並將它們命名爲事實上在標記中不同的對象是不必要的和令人困惑的。我不會推薦這樣做。另外,OP詢問如何獲取DataKeyField值,所以你應該在你的答案中包含它。 – 2015-01-21 16:57:43

相關問題