2014-10-10 59 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