Listbox Webcontrol SelectedIndexChanged Event not working

S

Scott Lemen

Hi,

My Listbox control is a webcontrol and its AutoPostBack property is set
to True. When I left-click on an item the event code here executes:

Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
If sender.selectedIndex > -1 Then
AddUserToDL(sender.selecteditem)
End If
End Sub

The problem is the sender.selectedIndex property is never greater than -1
and the sender.SelectedItem is set to Nothing. This happens whether the
control's SelectedMode is set to Single or Multiple and it does not matter
which of the 100 plus items, or multiple items, is selected. Does anyone
know what could cause this?

I am running Visual Studio 2003 Enterprise on a Windows 2000 Pro SP4
computer. The web server is IIS, which is running on my own workstation.

Thank you for your assistance.

Scott Lemen
 
L

lisa

Instead of sender.SelectedItem, try CType(sender,
ListBox).SelectedIndex. SelectedIndex isn't a property of Object, and
sender is an Object unless you convert it. If you're using VS.NET, the
fact that the S in SelectedIndex didn't become capitalized would have
been a tipoff.

Lisa
 
S

Scott Lemen

Lisa,

Thank you for replying but your solution did not solve the problem. Even
when the Event code is changed as shown just below, the
myListBox.SelectedIndex = -1 and .SelectedItem is Nothing. Also,
mySelectedListItemIndex = -1.

Regards,

Scott

Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
Dim mySelectedListItemIndex As Integer
Dim myListBox As ListBox
myListBox = CType(sender, ListBox)
mySelectedListItemIndex = CType(sender, ListBox).SelectedIndex

If mySelectedListItemIndex > -1 Then
AddUserToDL(myListBox.Items(mySelectedListItemIndex))
End If
End Sub
 
L

lisa

Hmm... well, I think (I'm not sure, but it's easy enough to check) that
CType(e, Integer) will give you the SelectedIndex. Other than that,
I'm stumped.

Lisa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top