Listbox Webcontrol SelectedIndexChanged Event not working

Discussion in 'ASP .Net Web Controls' started by Scott Lemen, Jun 28, 2005.

  1. Scott Lemen

    Scott Lemen Guest

    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
    Scott Lemen, Jun 28, 2005
    #1
    1. Advertising

  2. Scott Lemen

    Guest

    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


    Scott Lemen wrote:
    > 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
    , Jun 28, 2005
    #2
    1. Advertising

  3. Scott Lemen

    Scott Lemen Guest

    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


    "" wrote:

    > 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
    >
    >
    > Scott Lemen wrote:
    > > 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

    >
    >
    Scott Lemen, Jun 28, 2005
    #3
  4. Scott Lemen

    Guest

    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


    Scott Lemen wrote:
    > 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 myListBox As ListBox = CType(sender, ListBox)
    > Dim mySelectedListItemIndex As Integer = myListBox.SelectedIndex
    >
    > If mySelectedListItemIndex > -1 Then
    > AddUserToDL(myListBox.Items(mySelectedListItemIndex))
    > End If
    > End Sub
    >
    >
    > "" wrote:
    >
    > > 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
    > >
    > >
    > > Scott Lemen wrote:
    > > > 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

    > >
    > >
    , Jun 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward
    Replies:
    1
    Views:
    13,505
    Edward
    Feb 4, 2004
  2. CW
    Replies:
    2
    Views:
    7,045
  3. msnews.microsoft.com
    Replies:
    6
    Views:
    3,273
    msnews.microsoft.com
    Apr 28, 2005
  4. danob7
    Replies:
    1
    Views:
    864
    Brock Allen
    Jun 14, 2005
  5. studen771
    Replies:
    0
    Views:
    503
    studen771
    Mar 28, 2007
Loading...

Share This Page