简体   繁体   中英

code for visibility of listbox in 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

I am facing problem in above code. I want to make listbox visible when the value of dropdownlist get changed. Does any one know that?

Dropdown's SelectedIndexChange will fire every time you select a different item. But you are making the ListBox visible only when SelectedIndex =1. Remove the SelectedIndex condition like this:

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

And ListBox will be visible each time DropDown selection changes.

BTW: It is not clear how you are setting visibility of the listbox to false. Uou can post some markup and code to make it clear.

You can use the following code to get the listbox to appear on ANY change in the value of the dropdownlist

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

However if you want to change when the user selects the first/second or nth item, you can use this

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM