?
Solved

populating list box   in ASP.NET

Posted on 2006-04-04
8
Medium Priority
?
255 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

0
Comment
Question by:pratikshahse
8 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16372410
try this:

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

arr = variable1.Split(";")

MyListBox.DataSource = arr


AW
0
 

Author Comment

by:pratikshahse
ID: 16372570
what should be the mylistbox.datatextfield.

should it be

mylistbox.datatextfield = variable1
0
 
LVL 12

Expert Comment

by:UnexplainedWays
ID: 16372812
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:UnexplainedWays
ID: 16372832
" I tried pratikshahse's answer and it works."

I copied and pasted the wrong name, sorry Arthur_Wood.
0
 

Author Comment

by:pratikshahse
ID: 16372967
when i copy AW's code and paste it in page load my list box stay empty. nothing shows up
0
 
LVL 12

Accepted Solution

by:
UnexplainedWays earned 1000 total points
ID: 16373061
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);
}




0
 

Author Comment

by:pratikshahse
ID: 16373173
unexplained ways do you have anything in vb. not familiar with c# that much.

thank you
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16373381
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Six Sigma Control Plans
Introduction to Processes

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question