473,411 Members | 1,937 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,411 software developers and data experts.

ListBox not returning correct SelectedIndex

I'm going crazy with this. I just added two Listboxes (lbxSource->multiple
selection and lbxDestination -> single selection).
Then:
a) button "Load":
ListItem l0 = new ListItem("01");
lbxDestination.Items.Add(l0); //1st goes to Destination Box
ListItem l1 = new ListItem("02");
lbxSource.Items.Add(l1);
ListItem l2 = new ListItem("03");
lbxSource.Items.Add(l2);
ListItem l3 = new ListItem("04");
lbxSource.Items.Add(l3);
b) button "Insert" (to move an item from Source to Destination):
foreach (ListItem li in lbxSource.Items)
{
if (li.Selected)
{
lbxDestination.Items.Add(li);
lbxDestination.SelectedItem.Value = "01";
li.Selected = false; //otherwise I got a runtime error 'multiple not
allowed in s
}
}
c) a "Show" button that shows the context of the Selected Destination Item:
tbxText.Text = lbxDestination.SelectedItem.Text;

And it's unbelievable, but tbxText.Text always shows "01", no matter what I
selected.
I had this in a real world application and since I couldn't believe it I
wrote a small test-page and it behaves exactly the same.
Is there a workaround to this ? Or am I doing something wrong ?

Nov 19 '05 #1
1 2633
your step (b) is your problem
First off, chances are you want to create new instance of ListItems instead
of simply copying the reference...so instead of

add(li) do add(new ListItem(li.Text));

secondly, when you do SelectedItem.Value = "01" you are telling it to take
the selected item and putting it's value to 01....since you are copying
references (as above) each new item IS selected (since it was selected in
the previous checkbox) and you are overwriting its value to "01" hence no
matter which you select, they all have the same value...

your insert should look something like:

foreach (ListItem li in lbxSource.Items) {
if (li.Selected) {
lbxDestination.Items.Add(new ListItem(li.Text));
li.Selected = false;
}
}
lbxDestination.SelectedIndex = 0;
karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Carlo Marchesoni" <Ca*************@discussions.microsoft.com> wrote in
message news:34**********************************@microsof t.com...
I'm going crazy with this. I just added two Listboxes (lbxSource->multiple
selection and lbxDestination -> single selection).
Then:
a) button "Load":
ListItem l0 = new ListItem("01");
lbxDestination.Items.Add(l0); //1st goes to Destination Box
ListItem l1 = new ListItem("02");
lbxSource.Items.Add(l1);
ListItem l2 = new ListItem("03");
lbxSource.Items.Add(l2);
ListItem l3 = new ListItem("04");
lbxSource.Items.Add(l3);
b) button "Insert" (to move an item from Source to Destination):
foreach (ListItem li in lbxSource.Items)
{
if (li.Selected)
{
lbxDestination.Items.Add(li);
lbxDestination.SelectedItem.Value = "01";
li.Selected = false; //otherwise I got a runtime error 'multiple not allowed in s
}
}
c) a "Show" button that shows the context of the Selected Destination Item: tbxText.Text = lbxDestination.SelectedItem.Text;

And it's unbelievable, but tbxText.Text always shows "01", no matter what I selected.
I had this in a real world application and since I couldn't believe it I
wrote a small test-page and it behaves exactly the same.
Is there a workaround to this ? Or am I doing something wrong ?

Nov 19 '05 #2

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

Similar topics

2
by: James Goodman | last post by:
I have a listbox named sub1 on an asp page. I need to fill this list with values from a table. These are selected based upon the selection of a value/s in another listbox. It was suggested that I...
1
by: don | last post by:
Ref Dvd Title Location ---------------------------------------- 62 Pilates; Austin Binder 1 63 Yoga; Kripalu Binder 1 64 Yoga; Workout ...
3
by: CJM | last post by:
I would like to be able to use an autocompleting listbox - that is, one where you can select an item in the list by typing the first few characters; the standard IE listbox only responds to the...
3
by: John S | last post by:
How can you 1.) Get the text of an item selected? 2.) Get the index number of the item selected? 3.) Tell if the user clicked in a blank area?
1
by: Josema | last post by:
Hi to all, I have a class (persons) that derives from collection base: and another class (person) with this properties: -ID -Name When i have complete filled the object Persons with all...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
10
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.