By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

ListBox selected item

P: n/a
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>
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"David De Cotis" <mc**@mcms.com> wrote in message
news:26******************************@ureader.com. ..
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?
Nov 19 '05 #2

P: n/a
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" wrote:
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>

Nov 19 '05 #3

P: n/a
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

Nov 19 '05 #4

P: n/a
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.
Nov 19 '05 #5

P: n/a
Are you resetting something in the Page_Load method?

"David De Cotis" wrote:
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.

Nov 19 '05 #6

P: n/a
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
Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.