ListBox?

A

Arpan

Consider the following code that populates a ListBox:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
End If
Page.DataBind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="true" SelectionMode="single"
runat="server"/>
<asp:Label id="lblMessage" Text=<%# lbColors.SelectedItem.Text %>
runat="server"/>
</form>

The above code works fine as expected & populates the ListBox with the
different colors listed in the array one after the other i.e. the
ListBox looks like this:

red
blue
green
yellow

No problem till here but if the variable "arrColors" is declared as
just a string & NOT as a string array as shown in the above code & it
is assigned a value, say, "yellow" i.e. the "Dim arrColors()......"
line in the above code is replaced by

Dim arrColors As String = "yellow"

then the ListBox looks like this (keeping the rest of the code shown
above as it is):

y
e
l
l
o
w

Why so?

Thanks,

Arpan
 
G

Guest

Hi,
A string object is a collection of unicode characters that can be enumerated
over/through so I guess that if assigned directly as a datasource it is
interpreted as such. To add a single string you'd need to (apologies if you
know already) :

Dim arrColors As String = "yellow"
lbColors.Items.Add(arrColors)

Chris
 
A

Arpan

so I guess that if assigned directly as a datasource it is
interpreted as such

Since you are saying that your response is just a "guess", can I
conclude that you aren't 100% sure about your answer? I did prefer
getting a concrete answer to my post, my dear friend!

Thanks,

Regards,

Arpan
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top