473,412 Members | 5,361 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,412 software developers and data experts.

Listbox - if (item.selected) not responding as expected

while processing the items in a listbox foreach loop, the if (item.selected) never finds expected row(s)

The foreach is looping thru the data as expected.

Expand|Select|Wrap|Line Numbers
  1. <asp:ListBox runat="server" ID="lstFrom" Width="250" Height="200" SelectionMode="Multiple"></asp:ListBox>
  2. <asp:ListBox runat="server" ID="lstTo" Width="250" Height="200" SelectionMode="Multiple"></asp:ListBox>
  3.  
  4. protected void btnSelectOne_Click(object sender, EventArgs e)
  5.     {
  6.         MoveSelectedItem(lstFrom, lstTo);
  7.     }
  8.  
  9. private void MoveSelectedItem(ListBox fromListBox, ListBox toListBox)
  10.     {
  11.         List<ListItem> selected = new List<ListItem>();
  12.  
  13.         foreach (ListItem item in fromListBox.Items)
  14.         {
  15.             if (item.Selected) selected.Add(item);          // this is not being hit
  16.         }
  17.     }
Aug 27 '15 #1
1 1484
A few problems could cause the if(item.selected) line to not be hit:

1. The code snippet doesn't demonstrate where fromListBox is initialized with values.
2. The data type is not correctly identified. Perhaps use a var type such as

foreach (var item in fromListBox.Items)
{
if (item.Selected) selected.Add(item);
}
Aug 31 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dave | last post by:
Hi All, Just getting started with ASP (NOT .NET) and have a question: I have a basic form and I'm trying to add some functionality to it. I want a user to be able to select a product from a...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
0
by: E Miller | last post by:
Visual Studio .NET 2002 I've got an application in which I need to be able to click on a listbox item to initiate a dialog pertaining to that item. I'd like to be able to keep the dialog...
1
by: BillyB | last post by:
I have a context menu tied to a list box. When I right-click on an item, I'd like that item to be selected before the context menu invokes, similar to how Outlook Express works when right-clicking...
2
by: baret bonden | last post by:
Trying to return a selected listbox item to another form .tried lots of ways; defining public variables and passing those as well as textboxes ..I' m able to display the chosen item on it's form...
1
by: dev.amit | last post by:
Hello friends Can anyone tell me how to deselect the item selected in the list box If Me!lb_test_property.ItemsSelected.Count > 0 Then For Each varItem In Me!lb_test_property.ItemsSelected...
5
by: juengelj | last post by:
I have four listboxes on my form. When I add a new record, I run the following code to clear list box items... Public Function ClearAll() 'This function clears ALL selected fields in the form's...
0
by: hennas | last post by:
Basically i want to design a membership Name and Telephone List form using the following command buttons. Edit Add New; Update; Delete; Cancel; Save; Clear, and Exit
1
by: 9623014150 | last post by:
Hello there I always see it on forums and stuff, when you go to edit your profile, your selected listbox item or whatever you put in a dropdown menu is shown to you, exactly how you selected it. ...
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: 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...
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.