473,499 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 1486
Kara Hewett
27 New Member
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
2538
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
3586
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
1343
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
1661
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
4850
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
6513
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
4692
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
1353
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
1842
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
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6899
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
7390
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
5475
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,...
1
4919
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...
0
4602
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...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.