Problems dynamically adding items to Listbox

P

Papa.Coen

I've just spend a lot of time solving the following problem:
I used dotNet 2.0 / Visualstudio 2005 / C# / aspx, enable viewstate is
set on all controls.
I have 2 listboxes; the left contains items that can be (dynamically)
added to the right box.
Adding the first item went fine, it shows up in the right box.
Adding the second item resulted in losing _every_ control following the
right box. 'View source' reveiled that the rendering of the page stoped
at the <select of the second item in the box.
The box also showed only the last added item and a 'blank' entry.
Adding a third item resulted in an error message 'XML Parsing Error: no
element found' (at least in FireFox, IE just showed a blank page :( )

The solution:
(RE)SET THE SELECTEDINDEX OF THE LISTBOX AFTER AN ITEM IS ADDED!

Why? beats me. I suspect that because of the lame state/event handling
structure in a webpage there are somehow TWO selected indexes available
for ONE listbox...
 
P

Papa.Coen

An example which also shows the idiocracy of the solution, the
following is required:

(box already contains items)

this.listHandlers.Items.Insert(this.listHandlers.SelectedIndex,
this.listSelect.SelectedItem);
this.listHandlers.SelectedIndex = this.listHandlers.SelectedIndex;

(original/proofed sample code)
 
E

Eliyahu Goldin

When you move items from box to another, do you remove them from the first
box? Items can't be shared between two boxes, they can belong only to one.

Eliyahu
 
P

Papa.Coen

I do not remove the items in the first box.
There seem to be no problems, even when I add more than one instance of
the first into the second box.
 
E

Eliyahu Goldin

There is no problem if you add another instance of ListItem to the second
box. The problem starts when you refer to the same instance from two boxes.
That is when the SelectedIndex property gets confused. ListItem has its own
Selected property. When you set SelectedIndex on the ListBox, it sets
Selected property of the corresponding ListItem. Now you set SelectedIndex
property of the second ListBox to some other value. The second ListBox marks
the same ListItem as unselected. The first ListBox knows nothing about what
has the second ListBox done and keeps thinking the item is selected. This
produces confusion.

Eliyahu
 
P

Papa.Coen

You are absolutely right! Me and my big mouth ;) (but still the
behaviour remains strange)

I substituted the replacement as follows:
from
Box.Add/Insert(SelectedItem)
to
Box.Add/Insert(new Item(SelectedItem.Val))

Do you by any chance also know if it is somehow possible to store
object in a ListBox? I've already discovered that both ListItemColl &
ListItem can not be used as a superclass to create some listbox object
container.
 
E

Eliyahu Goldin

No. It is designed for ListItem collection only. But you should be able to
use a repeater for the same functionality.

Eliyahu
 

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

Top