vb.net中列表框可见性的代码

时间:2013-09-13 06:36:33

标签: asp.net vb.net

  Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
        MsgBox("OK")
        If (DropDownList2.SelectedIndex) = 1 Then
            ListBox1.Visible = True
        End If
    End Sub

我在上面的代码中遇到问题。我想在dropdownlist的值发生变化时使列表框可见。有人知道吗?

2 个答案:

答案 0 :(得分:1)

Dropdown的SelectedIndexChange将在您每次选择其他项目时触发。但是,只有当SelectedIndex = 1时,才能使ListBox可见。删除SelectedIndex条件,如下所示:

Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
        'MsgBox("OK")
        ListBox1.Visible = True
End Sub

每次DropDown选择更改时,ListBox都会可见。

BTW:目前尚不清楚如何将列表框的可见性设置为false。你可以发布一些标记和代码来说清楚。

答案 1 :(得分:0)

您可以使用以下代码让列表框显示在下拉列表值的任何更改

Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged

Dim cs As ClientScriptManager = Page.ClientScript

 cs.RegisterClientScriptBlock(Me.GetType(), "MyScript", "<script type=""text/javascript""> Alert("Ok"); </script>", False);
 ListBox1.Visible = True
    End Sub

但是,如果您想在用户选择第一个/第二个或第n个项目时进行更改,则可以使用此

 Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged

    Dim cs As ClientScriptManager = Page.ClientScript

     cs.RegisterClientScriptBlock(Me.GetType(), "MyScript", "<script type=""text/javascript""> Alert("Ok"); </script>", False);


if DropDownList2.SelectedIndex = 0  //makes the listbox visible only when you select the first item, Use 1 for making the list box visible on the selection of the second item, so on and so forth.
     ListBox1.Visible = True
end if

        End Sub