Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ListBox selected item

Reply
Thread Tools

ListBox selected item

 
 
David De Cotis
Guest
Posts: n/a
 
      06-15-2005
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>
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q3JhaWc=?=
Guest
Posts: n/a
 
      06-15-2005
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>
>

 
Reply With Quote
 
 
 
 
Seefor
Guest
Posts: n/a
 
      06-15-2005

"David De Cotis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> 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?


 
Reply With Quote
 
Aaron Corcoran
Guest
Posts: n/a
 
      06-15-2005
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

 
Reply With Quote
 
David De Cotis
Guest
Posts: n/a
 
      06-15-2005
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.
 
Reply With Quote
 
=?Utf-8?B?Q3JhaWc=?=
Guest
Posts: n/a
 
      06-15-2005
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.
>

 
Reply With Quote
 
David De Cotis
Guest
Posts: n/a
 
      06-15-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
Listbox move multiple selected items to second listbox K B ASP .Net 2 01-08-2007 11:16 AM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
selected item and dropdown list/listbox rohith ASP .Net 3 08-29-2003 04:32 PM
Get the selected item from a listbox Craig Buchanan ASP .Net 0 06-26-2003 12:47 AM



Advertisments