ListBox selected item

  • Thread starter Thread starter David De Cotis
  • Start date Start date
D

David De Cotis

Hello all, I am trying to go through a ListBox and verify if am item was
selected. If an item was selected, I would like to get a handle of the item

and simply do a response.write on the selected handle. The issue that I am

facing is that I am going each item, but my code does not catch the selected

item. Can anyone please take a look at the code and let me know what I am
doing wrong.

If listBox1.Items.Count > 0 Then
For intCount = 0 To listBox1.Items.Count - 1
If listBox1.SelectedIndex = True Then
Response.Write(listBox1.SelectedIndex)
End If
Next
End If


B.T.W. this is the way the listBox is defined in the .NET HTML page:

<asp:listbox id="listBox1" runat="server" Width="200px"
Height="142px"></asp:listbox><BR>
 
You don't need to loop through the listbox, just use listBox1.SelectedItem,
and by the way, listBox1.SelectedIndex isn't a true/false, it's the index
number, starting with 0 of the item selected. So, if the first item was
selected, SelectedIndex would be 0, and if the 8th item was selected,
SelectedIndex would be 7.
 
David De Cotis said:
Hello all, I am trying to go through a ListBox and verify if am item was
selected. If an item was selected, I would like to get a handle of the
item

and simply do a response.write on the selected handle. The issue that I
am

facing is that I am going each item, but my code does not catch the
selected

item. Can anyone please take a look at the code and let me know what I am
doing wrong.

If listBox1.Items.Count > 0 Then
For intCount = 0 To listBox1.Items.Count - 1
If listBox1.SelectedIndex = True Then
Response.Write(listBox1.SelectedIndex)
End If
Next
End If

Um, the listBox1.SelectedIndex holds the index of the selected item, so what
are you trying to do?
 
David,

If you are trying to catch this information in the selectedindexchanged
event, ensure that the listbox's autopostback feature is enabled. I
was unsure if when you were trying to capture this information, so
simply wanted to put that out there in case that method was being
attempted.

Aaron
 
Thanks, that is what I thought, but I got sidetracked because when I click
on the button getSelectedItem_Click (see code below) the page seems to
refresh and thereafter the selection that I made on the listbox is not
selected anymore and also the response.write only writes "You have selected
the following item: " and nothing else. It is like when I click on the
button getSelectedItem_Click everything is resetted. Can anyone please give
me an explaination.


Private Sub getSelectedItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles getSelectedItem.Click
Response.Write("You have selected the following item: ")
Response.Write(listBox1.SelectedItem)
End Sub


Many thanks to all.
 
Thank-you Craig, You hit it right on the mark. In the Page_Load method I
was reading my listbox entries from another posting and thereby creating the
new listbox for the current posting. Obviously, then my selectedindex would
always return nothing. when I place
Response.Write("You have selected the following item: ")
Response.Write(act_quest_ListBox.SelectedItem) In the Page_Load method it
works. My question, how can I call my function from a button without having
my page reloaded????

Thank-you all
 

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