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
<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