We help IT Professionals succeed at work.

populating list box   in ASP.NET

pratikshahse
pratikshahse asked
on
Medium Priority
277 Views
Last Modified: 2010-05-18


i have a variable kind of like this

variable1 = item1;item2;item3;item4;

everytime there is a ";" i want to break the variable and put that item in listbox.

list box should look like this.

item1
item2
item3
item4

Comment
Watch Question

try this:

Dim arr as String()
Dim variable1 as String = "item1;item2;item3;item4"

arr = variable1.Split(";")

MyListBox.DataSource = arr


AW

Author

Commented:
what should be the mylistbox.datatextfield.

should it be

mylistbox.datatextfield = variable1
I tried pratikshahse's answer and it works.

In one line it is: listBox1.DataSource = textBox1.Text.Split(';');

There is no need to use datatextfield, DataSource  works.
" I tried pratikshahse's answer and it works."

I copied and pasted the wrong name, sorry Arthur_Wood.

Author

Commented:
when i copy AW's code and paste it in page load my list box stay empty. nothing shows up
Whoops, my bad, i did that in a windows app (i guess the 2 listboxes are not the same).

Have a wack @ this: (C#)

string str = "item1;item2;item3;item4";
string[] items = str.Split(';');
foreach(string item in items)
{
      ListBox1.Items.Add(item);
}




Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
unexplained ways do you have anything in vb. not familiar with c# that much.

thank you
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Not after any points for this, but here's a slightly shorter version. Not necessarily any better/faster, I just like as few a lines of code as possible :o)

        Dim variable1 As String = "item1;item2;item3;item4"
        For Each s As String In variable1.Split(";")
            ListBox1.Items.Add(s)
        Next
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.