1

Ok, so I know this works on windows based forms because I have used it numerous times.....

        lstBoxExternal.Items.AddRange(txtBoxNameExternal.Text.Split(vbNewLine))

But this does not work for we based forms I am guessing? Can anyone tell me why and what the correct way to add text to a list box from a textbox on button click?

Error

Value type of String cannot be converted to 1-dimensional array of System.Web.UI.WebControls.Listitem

1 Answer 1

0

The ASP.NET ListBox web control's AddRange() method expects an array of ListItem which is why it can't so easily be converted from Windows forms.

If you are using the data from the txtBoxNameExternal to completely replace whatever items may already be in the ListBox (as opposed to adding to the items) you can use DataBind() which is probably easiest method:

    lstBoxExternal.DataSource = txtBoxNameExternal.Text.Split(vbNewLine)
    lstBoxExternal.DataBind()

If you need to keep adding items on each button click there are a few ways you could do this but I usually do it by adding items in a loop:

Dim items As [String]() = txtBoxNameExternal.Text.Split(vbNewLine)
For Each item As [String] In items
    lstBoxExternal.Items.Add(New ListItem(item))
Next

Also see this related answer for some tips when binding string arrays to DropDownList/ListBox controls: Binding array of string to DropDownList?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.