473,698 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having issues with listbox, probably simple....

Hey guys,

I've added a list of items to a listbox in my C#/ASP web form. I allow
the user to select a few items, and then click a button. I want to be able to
retrieve the items from the list box that THAT user has selected and write it
to a string.

This is what I'm doing to read the text and it's not working:
protected void Button1_Click(o bject sender, EventArgs e)
{

string strTemp;
foreach (ListItem lstItem in ListBox1.Items)
{
strTemp = lstItem.Text;
if (lstItem.Select ed = true)
{
strTotString = strTotString + "," + strTemp;
}
}
}
Any idea what I'm doing wrong?

Not matter how many or what I highlight, it keeps giving me the very first
item in the list box (whether or not I've actually highlighted it).
Thanks!

Oct 6 '06 #1
2 1063

"Todd Jaspers" <To*********@di scussions.micro soft.comwrote in message
news:C2******** *************** ***********@mic rosoft.com...
Hey guys,

I've added a list of items to a listbox in my C#/ASP web form. I
allow
the user to select a few items, and then click a button. I want to be able
to
retrieve the items from the list box that THAT user has selected and write
it
to a string.

This is what I'm doing to read the text and it's not working:
protected void Button1_Click(o bject sender, EventArgs e)
{

string strTemp;
foreach (ListItem lstItem in ListBox1.Items)
{
strTemp = lstItem.Text;
if (lstItem.Select ed = true)
{
strTotString = strTotString + "," + strTemp;
}
}
}
Any idea what I'm doing wrong?

Not matter how many or what I highlight, it keeps giving me the very first
item in the list box (whether or not I've actually highlighted it).
Thanks!
2 things:

If there is a property named SelectedItems, I would use that to iterate
through.

Your IF statement is a little funky :) You have if (lstItem.Select ed =
true) instead of if (lstItem.Select ed == true). To avoid this, I just use
if (lstItem.Select ed) ... unless I have to of course ;)

HTH,
Mythran
Oct 6 '06 #2
Thanks Mythran, I appreciate the advice and the info.

However, it still does the same thing.
When I run my form, I'll highlight a couple of items on the list box, and
then click a button which runs this code:

protected void Button1_Click(o bject sender, EventArgs e)
{
string strTotString;
string strTemp;

strTotString = "";
foreach (ListItem lstItem in ListBox1.Items)
{
strTemp = lstItem.Text;
if (lstItem.Select ed == true)
{
strTotString = strTotString + "," + strTemp;
}
}
tBxComments.Tex t = strTotString;
}
After that code is run, what actually happens is that the VERY FIRST item in
my listbox is the ONLY item that will end up being the value of
strTotString... EVEN if I didn't select that item.

What I basically want to do is retrieve a list of all the items that I've
selected (highlighted) from a listbox that I have. What could I be doing
wrong? I just don't get it.

Thanks!!!

Todd

Oct 6 '06 #3

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

Similar topics

5
540
by: Brian | last post by:
Hi, All, I'm using MS.net 2003 and using a windows.forms.listbox control for my window application but I don't know why the Horizontal Scrollbar could NOT be shown even if I set HorizontalScrollbar = true. The text info in listbox already beyond its right edge. How can I see the hidden part info in listbox? here is a part of codes. ListBox.ValueMember = XX ListBox.DisplayMember = XXXX
1
5023
by: Alvey Sidecast | last post by:
This is probably embarrassingly simple, but I've been trawling through this ng for hours now and my brain hurts. I've got an unbound multi-column listbox (multi-select=none) whose rowsource is a query based on a combobox selection. After the selection is made from the combobox the listbox may, or may not, have records in it. If it doesn't, no problems, I've got; If Me.lbxContacts.ItemsSelected.Count = 0 Then
2
2217
by: Alex Simic | last post by:
Dear all I am very desperate with my problem and you might be probably my last chances for this issue. I have a listbox which is binded to a DataTable. I would like in the program to select in this listbox some items. I try to do this with following (of course much more profesional - but let's keep it simple):
7
4531
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 ASP.Net stuff when I need a Windows Form control. I've seen dual listbox populators in countless Windows applications, and have seen them run very fast, so I figured this would be extremely popular. Here's how it should work:
1
5050
by: zoneal | last post by:
Hi I have a form with a listbox, two comboboxes and two Radiobuttons. What I want to do is make a selection from both comboboxes and either Radiobutton and display these three elements across the top of the listbox eg. 5, 10, 'Go' As I need multiple rows of data I have a button to 'Load' this to the listbox. I'm stuck though as I'm having difficulty loading this correctly. Question: Woulkd I be better off with a datagrid for this job,...
4
2795
by: Jeff | last post by:
....still new at this. ...hopefully a simple question Is there some practical way of altering the function of a keypress in Visual Web.net 2005 using VB without causing a postback on each keypress? For example: A have a listbox with a number of selections. The up and down keyboard keys change the indicated listbox selection and by default the <tabkey takes the focus away from the listbox and onto a button that will submit the selection. I'm...
2
2095
by: Steve Potter | last post by:
I am trying to find some method of attaching a Listbox object to a list object so as the contents of the list are changed the contents of the Listbox will be updated to match. I have found a few references to something like this in this old post http://groups.google.com/group/comp.lang.python/browse_thread/thread/3a6fe7534c812f55/43f201ab53cfdbf7 as well as here http://effbot.org/zone/wck-4.htm . It just seems that there should be some...
3
1274
by: Carolyn | last post by:
I am new to using Access and am trying to populate a listbox based on another listbox. My first box has the name of various customers. I have another listbox that populates the service location. This works fine most of the time. The problem is, one of the customers has multiple sites. I am not allowed to write over the service location and do not want to list the customer more than once in the customer box. Does someone have a way to...
8
6032
by: PeteOlcott | last post by:
http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/grid/tests/test_change_structure.html I would like to completely understand how GUI controls such as this one are constructed. In the ideal case I would like to see all of the source code for a complete working example of a ListBox of CheckBoxes.
0
8611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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 we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.