gridview页脚中的下拉列表框

时间:2014-10-10 16:30:15

标签: asp.net vb.net gridview

我在Gridview的页脚中有2个DropDownList框,我试图根据第一个框中的选择更新另一个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 个答案:

答案 0 :(得分: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));
}