473,382 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problems dynamically adding items to Listbox

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

Mar 8 '06 #1
6 3645
An example which also shows the idiocracy of the solution, the
following is required:

(box already contains items)

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

(original/proofed sample code)

Mar 8 '06 #2
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

"Papa.Coen" <pa*******@gmail.com> wrote in message
news:11**********************@j52g2000cwj.googlegr oups.com...
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...

Mar 8 '06 #3
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.

Mar 8 '06 #4
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

"Papa.Coen" <pa*******@gmail.com> wrote in message
news:11*********************@p10g2000cwp.googlegro ups.com...
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.

Mar 8 '06 #5
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.

Mar 8 '06 #6
No. It is designed for ListItem collection only. But you should be able to
use a repeater for the same functionality.

Eliyahu

"Papa.Coen" <pa*******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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.

Mar 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John R. | last post by:
I want to have a listbox that shows a checkbox and a textbox. I created a user control that has a checkbox and a textbox in it and have been trying to add it to a listbox but I can't get it to...
3
by: Ryan Taylor | last post by:
Hello. I have an application where I need the user to be able to add items to a listbox. I've implemented this via javascript. The listbox is an <asp:ListBox>. However, when the user submits the...
6
by: Sparky Arbuckle | last post by:
The code below works to an extent. The problem with it is that no matter which item is selected, the first one in the list is always the item that gets added/removed. How can I alleviate this...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
9
by: Suman | last post by:
Happy Friday everyone!!! I am working on a windows service and a C# application and needed some help with certain functionality. Please read through my issue below. Thanks! I have a windows...
0
by: rn5a | last post by:
I have 2 ListBoxes which get populated with records from a database. The Form has a Button as well. Note that users can select multiple items in both the ListBoxes. When a user selects items...
1
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
2
by: =?Utf-8?B?TWFobW91ZCBTaGFiYW4=?= | last post by:
i have a problem in adding new listbox items i don't need to allow adding multible items with the same textvalue ex: if current items are: green red
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.