Having issues with listbox, probably simple....

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
Todd Jaspers said:
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top