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

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(object sender, EventArgs e)
{

string strTemp;
foreach (ListItem lstItem in ListBox1.Items)
{
strTemp = lstItem.Text;
if (lstItem.Selected = 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 1057

"Todd Jaspers" <To*********@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.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(object sender, EventArgs e)
{

string strTemp;
foreach (ListItem lstItem in ListBox1.Items)
{
strTemp = lstItem.Text;
if (lstItem.Selected = 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.Selected =
true) instead of if (lstItem.Selected == true). To avoid this, I just use
if (lstItem.Selected) ... 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(object sender, EventArgs e)
{
string strTotString;
string strTemp;

strTotString = "";
foreach (ListItem lstItem in ListBox1.Items)
{
strTemp = lstItem.Text;
if (lstItem.Selected == true)
{
strTotString = strTotString + "," + strTemp;
}
}
tBxComments.Text = 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
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...
1
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...
2
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...
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...
1
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...
4
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...
2
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...
3
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. ...
8
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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...

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.