ListBox selected item

Discussion in 'ASP .Net' started by David De Cotis, Jun 15, 2005.

  1. 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>
     
    David De Cotis, Jun 15, 2005
    #1
    1. Advertising

  2. 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>
    >
     
    =?Utf-8?B?Q3JhaWc=?=, Jun 15, 2005
    #2
    1. Advertising

  3. David De Cotis

    Seefor Guest

    "David De Cotis" <> wrote in message
    news:...
    > 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?
     
    Seefor, Jun 15, 2005
    #3
  4. 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
     
    Aaron Corcoran, Jun 15, 2005
    #4
  5. 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.
     
    David De Cotis, Jun 15, 2005
    #5
  6. 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.
    >
     
    =?Utf-8?B?Q3JhaWc=?=, Jun 15, 2005
    #6
  7. 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
     
    David De Cotis, Jun 15, 2005
    #7
    1. Advertising

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. Craig Buchanan

    Get the selected item from a listbox

    Craig Buchanan, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,879
    Craig Buchanan
    Jun 26, 2003
  2. rohith
    Replies:
    3
    Views:
    6,011
    rohith
    Aug 29, 2003
  3. Iain
    Replies:
    3
    Views:
    951
  4. K B
    Replies:
    2
    Views:
    683
  5. mldardy
    Replies:
    0
    Views:
    980
    mldardy
    Sep 28, 2010
Loading...

Share This Page