ListBox index values

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,

I would like to display the values of the index as chosen by the user in a
listbox. These values would be as per the datavaluefield. On testing I get
a value of -1:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.Label1.Text = ListBox1.Items.Item(2).Text
End Sub

I would like to replace the "2" with the item the user selects. Any help
appreciated.
 
hi,
you can get the selected value of the listbox at the server side using
ListBox1.SelectedItem.Value .
so try using Me.Label1.Text = ListBox1.SelectedItem.Value
 
Thanks but I get an error:

Object reference not set to an instance of an object.
 
hi niggy,
are you binding the listbox at runtime? if so check that the listbox has
items after postback.. you are getting this error probably becoz the listbox
has no selected item.i think you are binding the listbox on postback.
 
I have autopostback=true and listbox1.databind() on page load. What now?
 
in the page load check for postback like
If Not Page.IsPostBack Then
 

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

Back
Top