2014-10-10 62 views
0

我有一個GridView的頁腳2 DropDownList框,我試圖根據在第一個框中所做的選擇更新另一個DropDownList框的值。在gridview頁腳dropdownlist框

基本上有2個DDL。用戶可以從任何一箇中挑選,並且所選值的互補值顯示在相對的框中。 (是的,所有互補值存在於對方框的列表中。)

這些框根據需要填充,但即使我處於SelectedIndexChanged事件中,我似乎無法找到控件。

每個DDL位於Gridview的唯一列中。

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) 
    Dim DDL1 As String = TryCast(GridView1.FooterRow.FindControl("DropDownList1"), DropDownList).SelectedValue 
    Dim DDL2 As DropDownList = TryCast(GridView1.FooterRow.FindControl("DropDownList2"), DropDownList) 

    DDL2.SelectedValue = DDL1 
End Sub 

和相對盒....

Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As EventArgs) 
    Dim DDL1 As DropDownList = TryCast(GridView1.FooterRow.FindControl("DropDownList1"), DropDownList) 
    Dim DDL2 As String = TryCast(GridView1.FooterRow.FindControl("DropDownList2"), DropDownList).SelectedValue 


    DDL1.SelectedValue = DDL2 
End Sub 

回答

1

你需要做的是有點不同。這是工作版本。 sender包含對觸發事件的下拉列表的引用。您可以找到相應的行,然後查找該行中的其他下拉列表。你也不能直接設置SelectedValue作爲代碼中顯示的下拉列表,它必須如下所示。

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) 
    Dim DDL1 As DropDownList = DirectCast(sender, DropDownList) 
    Dim row As GridViewRow = DirectCast(DDL1.NamingContainer, GridViewRow) 
    Dim DDL2 As DropDownList = DirectCast(row.FindControl("DropDownList2"), DropDownList) 

    Dim SelectedValue As String = DDL1.SelectedItem.Value 
    DDL2.SelectedIndex = DDL2.Items.IndexOf(DDL2.Items.FindByValue(SelectedValue)) 
End Sub 

Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As System.EventArgs) 
    Dim DDL2 As DropDownList = DirectCast(sender, DropDownList) 
    Dim row As GridViewRow = DirectCast(DDL2.NamingContainer, GridViewRow) 
    Dim DDL1 As DropDownList = DirectCast(row.FindControl("DropDownList1"), DropDownList) 

    Dim SelectedValue As String = DDL2.SelectedItem.Value 
    DDL1.SelectedIndex = DDL1.Items.IndexOf(DDL1.Items.FindByValue(SelectedValue)) 
End Sub 

我用一個工具將我的C#代碼轉換爲VB.NET。原始的C#代碼是

protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    DropDownList DDL1 = (DropDownList)sender; 
    GridViewRow row = (GridViewRow)DDL1.NamingContainer; 
    DropDownList DDL2 = (DropDownList)row.FindControl("DropDownList2"); 

    string SelectedValue = DDL1.SelectedItem.Value; 
    DDL2.SelectedIndex = DDL2.Items.IndexOf(DDL2.Items.FindByValue(SelectedValue)); 
} 

protected void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    DropDownList DDL2 = (DropDownList)sender; 
    GridViewRow row = (GridViewRow)DDL2.NamingContainer; 
    DropDownList DDL1 = (DropDownList)row.FindControl("DropDownList1"); 

    string SelectedValue = DDL2.SelectedItem.Value; 
    DDL1.SelectedIndex = DDL1.Items.IndexOf(DDL1.Items.FindByValue(SelectedValue)); 
} 
+0

謝謝,丹尼斯。這看起來不錯,讓我插入並看看會發生什麼。 – htm11h 2014-10-10 17:56:32

+0

好吧,我在兩個事件中的SelectedIndex都保持爲零。我需要重新綁定嗎?該行返回-1 DDL2.Items.IndexOf(DDL2.Items.FindByValue(SelectedValue))。我知道價值在列表中。我可以看到它,如果我選擇反對下拉。兩項活動都相同。 – htm11h 2014-10-10 17:59:32

+0

好的,我轉換爲使用SelectedIndex,我知道它的工作原理。感謝您的幫助!!! – htm11h 2014-10-10 18:03:32