Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

How to pass data from Listbox to a textbox

Hello,

I am using the code below to pass data from my testbox to a list box, How do I pass the data from my listbox to a textbox separated by a (;).

C1NSN.Text = dtNSN.Rows(0).Item("NSN")
        Dim tmp() As String = C1NSN.Text.split(";")
        For i As Integer = 0 To tmp.Length - 1
            ListBox1.Items.Add(tmp(i))
         Next


Thanks,

Victor
Avatar of gplana
gplana
Flag of Spain image

Try something like this:

C1NSN.Text = ""
For i = 0 to listbox.items.count-1
   C1NSN.Text = C1NSN.Text & ";" & ListBox1.Items(0).Text
next i

Open in new window


Hope it helps
Do:
For x = 0 To LBox.Items.Count - 1
    C1NSN.Text = If(String.IsNullOrEmpty(C1NSN.Text.ToString.Trim), LBox.Items(x), C1NSN.Text & ";" & LBox.Items(x))
Next

Open in new window

Avatar of Victor  Charles

ASKER

Thank you, I will try them and get back you. How do I accomplish the same with all the rows in a  GridView?
Hi,

I tried both of them but it is still not working, the following code:

C1NSN.Text = ""
For i = 0 to listbox.items.count-1
   C1NSN.Text = C1NSN.Text & ";" & ListBox1.Items(0).Text
next i


Gives me the data of the first row multiple times, for example if my list box contains

AAAAA
BBBBBB
CCCCCC

My textbox shows:
AAAAA
AAAAA
AAAAA


The following code:

For x = 0 To LBox.Items.Count - 1
    C1NSN.Text = If(String.IsNullOrEmpty(C1NSN.Text.ToString.Trim), LBox.Items(x), C1NSN.Text & ";" & LBox.Items(x))
Next

Give me error message:

Operator '&' is not defined for types 'String' and 'System.Web.UI.WebControls.ListItem'.
get the listbox item text instead
For x = 0 To ListBox1.Items.Count - 1
    C1NSN.Text = If(String.IsNullOrEmpty(C1NSN.Text.ToString.Trim), ListBox1.Items(x).Text, C1NSN.Text & ";" & ListBox1.Items(x).Text)
Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gplana
gplana
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It worked! Thanks!

Do you know how to also do the same with a gridwiew?

Victor
Happy to know it worked. Sorry, I haven't work with gridview. I suggest to open another question for this and add the code you have on this new question.