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

R

Ron

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
 
L

Lowell Heddings

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
 
R

Ron

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.
 
J

Jo Inferis

Ron said:
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
 
L

Lowell Heddings

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
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top