user2771784
user2771784

Reputation: 1

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?

Upvotes: 0

Views: 689

Answers (2)

Farax
Farax

Reputation: 1477

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

Upvotes: 0

afzalulh
afzalulh

Reputation: 7943

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.

Upvotes: 1

Related Questions