ASP.NET/C# - Listbox.Items.Remove only selecting index 0

Discussion in 'ASP .Net' started by Ron, Dec 8, 2004.

  1. Ron

    Ron Guest

    I've got a listbox that holds a list of groups. Users can
    select a group, hit the remove button and the group
    should be removed from the listbox. The only problem is
    that no matter which group you select, the first one in
    the listbox is always removed.(The listitem with an index
    of 0. Box is set to single selection mode) I've looked at
    multiple examples and they all do it this way. What's
    wrong? (variables are also being set to the values of the
    first item)


    private void RemoveGroupButton_Click(object sender,
    System.EventArgs e)
    {
    string groupName;
    string toolID;

    groupName = GroupsListBox.SelectedItem.ToString();
    toolID = GroupsListBox.SelectedValue;

    //Remove from the listbox
    //************************************************
    **********************
    GroupsListBox.Items.Remove
    (GroupsListBox.SelectedItem);
    //************************************************
    **********************

    }//End Button
     
    Ron, Dec 8, 2004
    #1
    1. Advertising

  2. Have you tried something like this:

    GroupsListBox.Items.RemoveAt(GroupsListBox.SelectedIndex);

    Or even as a test:

    GroupsListBox.Items.RemoveAt(2);

    Also, you should be checking to make sure that an item is actually
    selected before allowing the user to remove items. Otherwise the system
    will throw an exception when there are no more items left and the user
    clicks the remove button.

    Lowell





    Ron wrote:
    > I've got a listbox that holds a list of groups. Users can
    > select a group, hit the remove button and the group
    > should be removed from the listbox. The only problem is
    > that no matter which group you select, the first one in
    > the listbox is always removed.(The listitem with an index
    > of 0. Box is set to single selection mode) I've looked at
    > multiple examples and they all do it this way. What's
    > wrong? (variables are also being set to the values of the
    > first item)
    >
    >
    > private void RemoveGroupButton_Click(object sender,
    > System.EventArgs e)
    > {
    > string groupName;
    > string toolID;
    >
    > groupName = GroupsListBox.SelectedItem.ToString();
    > toolID = GroupsListBox.SelectedValue;
    >
    > //Remove from the listbox
    > //************************************************
    > **********************
    > GroupsListBox.Items.Remove
    > (GroupsListBox.SelectedItem);
    > //************************************************
    > **********************
    >
    > }//End Button
     
    Lowell Heddings, Dec 8, 2004
    #2
    1. Advertising

  3. Ron

    Ron Guest

    I've already tried what you suggest, and when I do that,
    when I have any item selected, it's returning an index of
    0. If I don't select anything and hit the remove, it'll
    generate an exception.

    You suggested the GroupsListBox.Items.RemoveAt(2) code,
    and this works provided there is something with an index
    of 2. The whole problem I'm having is that I'm always
    being returned an index of 0, not matter which item is
    picked.

    >-----Original Message-----
    >Have you tried something like this:
    >
    >GroupsListBox.Items.RemoveAt

    (GroupsListBox.SelectedIndex);
    >
    >Or even as a test:
    >
    >GroupsListBox.Items.RemoveAt(2);
    >
    >Also, you should be checking to make sure that an item

    is actually
    >selected before allowing the user to remove items.

    Otherwise the system
    >will throw an exception when there are no more items

    left and the user
    >clicks the remove button.
    >
    >Lowell
    >
    >
    >
    >
    >
    >Ron wrote:
    >> I've got a listbox that holds a list of groups. Users

    can
    >> select a group, hit the remove button and the group
    >> should be removed from the listbox. The only problem

    is
    >> that no matter which group you select, the first one

    in
    >> the listbox is always removed.(The listitem with an

    index
    >> of 0. Box is set to single selection mode) I've looked

    at
    >> multiple examples and they all do it this way. What's
    >> wrong? (variables are also being set to the values of

    the
    >> first item)
    >>
    >>
    >> private void RemoveGroupButton_Click(object sender,
    >> System.EventArgs e)
    >> {
    >> string groupName;
    >> string toolID;
    >>
    >> groupName = GroupsListBox.SelectedItem.ToString();
    >> toolID = GroupsListBox.SelectedValue;
    >>
    >> //Remove from the listbox
    >> //*********************************************

    ***
    >> **********************
    >> GroupsListBox.Items.Remove
    >> (GroupsListBox.SelectedItem);
    >> //*********************************************

    ***
    >> **********************
    >>
    >> }//End Button

    >.
    >
     
    Ron, Dec 9, 2004
    #3
  4. Ron

    Jo Inferis Guest

    Ron wrote:
    > I've got a listbox that holds a list of groups. Users can
    > select a group, hit the remove button and the group
    > should be removed from the listbox.

    I've got some code that does this kind of thing if it helps (in my case
    there are two listboxes and you can move the items between the listboxes
    using "add" and "remove" buttons, but there may be something here you can
    use)

    <code snippet>
    protected void btnRemoveItems_Click(Object sender, ImageClickEventArgs e)
    {
    //ADD TO AVAILABLE ITEMS
    ListItemCollection toRemove = new ListItemCollection();
    foreach (ListItem item in lstAvailable.Items)
    {
    if (item.Selected)
    {
    lstAvailable.Items.Add(new ListItem(item.Text, item.Value));
    toRemove.Add(item);
    }
    }

    //REMOVE FROM SELECTED
    foreach (ListItem item in toRemove)
    {
    lstSelected.Items.Remove(item);
    }
    }
    </code snippet>

    Obviously there's another function which adds the items to the "selected
    items" listbox but it just does the same as this function with the listbox
    names swapped (which reminds me, I could probably turn this into one
    function that takes two listboxes as parameters...hmm..)

    Anyway...this kind of approach may help.

    There is also the possibility that you're binding the contents of the
    listbox before the click event is being fired (if you're doing this in
    Page_Load and not checking IsPostBack, then this is almost certainly your
    problem).

    HTH

    --
    jo inferis
     
    Jo Inferis, Dec 9, 2004
    #4
  5. I just noticed that this was an ASP.NET listbox and not Windows Forms...
    doh!

    Are you rebinding the listbox on the page postback? You need to make
    sure that on the postback you aren't rebinding the listbox, and also
    that you have the viewstate enabled for the control(Which is how .NET
    keeps the list of items even when you don't rebind it.)

    I suspect that is your problem.

    Lowell



    Ron wrote:
    > I've already tried what you suggest, and when I do that,
    > when I have any item selected, it's returning an index of
    > 0. If I don't select anything and hit the remove, it'll
    > generate an exception.
    >
    > You suggested the GroupsListBox.Items.RemoveAt(2) code,
    > and this works provided there is something with an index
    > of 2. The whole problem I'm having is that I'm always
    > being returned an index of 0, not matter which item is
    > picked.
    >
    >
    >>-----Original Message-----
    >>Have you tried something like this:
    >>
    >>GroupsListBox.Items.RemoveAt

    >
    > (GroupsListBox.SelectedIndex);
    >
    >>Or even as a test:
    >>
    >>GroupsListBox.Items.RemoveAt(2);
    >>
    >>Also, you should be checking to make sure that an item

    >
    > is actually
    >
    >>selected before allowing the user to remove items.

    >
    > Otherwise the system
    >
    >>will throw an exception when there are no more items

    >
    > left and the user
    >
    >>clicks the remove button.
    >>
    >>Lowell
    >>
    >>
    >>
    >>
    >>
    >>Ron wrote:
    >>
    >>>I've got a listbox that holds a list of groups. Users

    >
    > can
    >
    >>>select a group, hit the remove button and the group
    >>>should be removed from the listbox. The only problem

    >
    > is
    >
    >>>that no matter which group you select, the first one

    >
    > in
    >
    >>>the listbox is always removed.(The listitem with an

    >
    > index
    >
    >>>of 0. Box is set to single selection mode) I've looked

    >
    > at
    >
    >>>multiple examples and they all do it this way. What's
    >>>wrong? (variables are also being set to the values of

    >
    > the
    >
    >>>first item)
    >>>
    >>>
    >>>private void RemoveGroupButton_Click(object sender,
    >>>System.EventArgs e)
    >>>{
    >>> string groupName;
    >>> string toolID;
    >>>
    >>> groupName = GroupsListBox.SelectedItem.ToString();
    >>> toolID = GroupsListBox.SelectedValue;
    >>>
    >>> //Remove from the listbox
    >>> //*********************************************

    >
    > ***
    >
    >>>**********************
    >>> GroupsListBox.Items.Remove
    >>>(GroupsListBox.SelectedItem);
    >>> //*********************************************

    >
    > ***
    >
    >>>**********************
    >>>
    >>>}//End Button

    >>
    >>.
    >>
     
    Lowell Heddings, Dec 10, 2004
    #5
    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. kent
    Replies:
    1
    Views:
    900
    Andrew de la Harpe
    May 3, 2004
  2. =?Utf-8?B?U2lsZWVzaA==?=

    Urgent!!!!! Remove Items from a <Asp:Listbox> thru javascript

    =?Utf-8?B?U2lsZWVzaA==?=, Nov 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,114
    Curt_C [MVP]
    Nov 4, 2004
  3. K B
    Replies:
    2
    Views:
    693
  4. Tomas
    Replies:
    1
    Views:
    595
    Munna
    Aug 12, 2008
  5. Mark

    Selecting items in a listbox

    Mark, Apr 16, 2004, in forum: Javascript
    Replies:
    3
    Views:
    107
Loading...

Share This Page