Listbox NOT retrieving Selected Index

Discussion in 'ASP .Net' started by Chris Thunell, Jul 28, 2004.

  1. I have a Listbox on an vb aspx web page. When i select something in the
    listbox i do not get a selected value or selected index. The selected index
    is always -1.

    Any thoughts would be greatly appreciated!
    Chris Thunell


    Here is my code:
    loading:

    Me.lbPending.DataSource = dvPending 'dvpending is a dataview, but i've
    tried a dataset as well - no luck.
    Me.lbPending.DataTextField = "DateName"
    Me.lbPending.DataValueField = "BidNo"
    Me.lbPending.DataBind()

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button12.Click

    Label7.Text = (lbPending.SelectedItem.Value.ToString) 'this always
    returns -1

    End Sub
     
    Chris Thunell, Jul 28, 2004
    #1
    1. Advertising

  2. Just an additional thought.... both selectedvalue and selectedindex don't
    work.
    I created a new project with just the listbox and code similar to below -
    Nothin!
    Thanks again for your help!
    Chris Thunell


    "Chris Thunell" <> wrote in message
    news:%...
    > I have a Listbox on an vb aspx web page. When i select something in the
    > listbox i do not get a selected value or selected index. The selected

    index
    > is always -1.
    >
    > Any thoughts would be greatly appreciated!
    > Chris Thunell
    >
    >
    > Here is my code:
    > loading:
    >
    > Me.lbPending.DataSource = dvPending 'dvpending is a dataview, but i've
    > tried a dataset as well - no luck.
    > Me.lbPending.DataTextField = "DateName"
    > Me.lbPending.DataValueField = "BidNo"
    > Me.lbPending.DataBind()
    >
    > Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button12.Click
    >
    > Label7.Text = (lbPending.SelectedItem.Value.ToString) 'this always
    > returns -1
    >
    > End Sub
    >
    >
    >
    >
     
    Chris Thunell, Jul 28, 2004
    #2
    1. Advertising

  3. Chris Thunell

    Marina Guest

    If you are running the binding code every time, then that is the problem. If
    you are rebinding on postback, that discards all the current values (as well
    as the selection information) and rebinds the dropdown again.
    Make sure the binding code is in a 'If Not IsPostback Then' block.

    "Chris Thunell" <> wrote in message
    news:%...
    > I have a Listbox on an vb aspx web page. When i select something in the
    > listbox i do not get a selected value or selected index. The selected

    index
    > is always -1.
    >
    > Any thoughts would be greatly appreciated!
    > Chris Thunell
    >
    >
    > Here is my code:
    > loading:
    >
    > Me.lbPending.DataSource = dvPending 'dvpending is a dataview, but i've
    > tried a dataset as well - no luck.
    > Me.lbPending.DataTextField = "DateName"
    > Me.lbPending.DataValueField = "BidNo"
    > Me.lbPending.DataBind()
    >
    > Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button12.Click
    >
    > Label7.Text = (lbPending.SelectedItem.Value.ToString) 'this always
    > returns -1
    >
    > End Sub
    >
    >
    >
    >
     
    Marina, Jul 28, 2004
    #3
  4. That was it!!!! Geez something so simple.. i've spent 2 hours trying to
    figure this one out! :) Thank you so much for your help!

    "Marina" <> wrote in message
    news:...
    > If you are running the binding code every time, then that is the problem.

    If
    > you are rebinding on postback, that discards all the current values (as

    well
    > as the selection information) and rebinds the dropdown again.
    > Make sure the binding code is in a 'If Not IsPostback Then' block.
    >
    > "Chris Thunell" <> wrote in message
    > news:%...
    > > I have a Listbox on an vb aspx web page. When i select something in the
    > > listbox i do not get a selected value or selected index. The selected

    > index
    > > is always -1.
    > >
    > > Any thoughts would be greatly appreciated!
    > > Chris Thunell
    > >
    > >
    > > Here is my code:
    > > loading:
    > >
    > > Me.lbPending.DataSource = dvPending 'dvpending is a dataview, but i've
    > > tried a dataset as well - no luck.
    > > Me.lbPending.DataTextField = "DateName"
    > > Me.lbPending.DataValueField = "BidNo"
    > > Me.lbPending.DataBind()
    > >
    > > Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles Button12.Click
    > >
    > > Label7.Text = (lbPending.SelectedItem.Value.ToString) 'this always
    > > returns -1
    > >
    > > End Sub
    > >
    > >
    > >
    > >

    >
    >
     
    Chris Thunell, Jul 28, 2004
    #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. K B
    Replies:
    2
    Views:
    699
  2. mldardy
    Replies:
    0
    Views:
    1,001
    mldardy
    Sep 28, 2010
  3. haiwen
    Replies:
    3
    Views:
    194
    haiwen
    Jul 14, 2003
  4. Jacques

    Combo ListBox selected Index problem

    Jacques, Jul 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    192
  5. doni
    Replies:
    1
    Views:
    117
Loading...

Share This Page