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