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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    831
  2. Nathan Sokalski
    Replies:
    0
    Views:
    835
    Nathan Sokalski
    Oct 5, 2008
  3. mldardy
    Replies:
    0
    Views:
    1,174
    mldardy
    Sep 28, 2010
  4. haiwen
    Replies:
    3
    Views:
    279
    haiwen
    Jul 14, 2003
  5. Jacques

    Combo ListBox selected Index problem

    Jacques, Jul 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    367
  6. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    453
    Nathan Sokalski
    Oct 5, 2008
  7. doni
    Replies:
    1
    Views:
    182
  8. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    620
    Tomasz Chmielewski
    Mar 4, 2008
Loading...