S
Stephen Miller
When I dynamically populate a HtmlSelect combo box, the Value property
consistently fails to return the item selected, defaulting instead to
the first item in the list. For example:
Protected WithEvents Fruits As System.Web.UI.HtmlControls.HtmlSelect
Protected WithEvents Results As System.Web.UI.WebControls.Label
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
…
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim values As ArrayList = New ArrayList()
values.Add("Apple")
values.Add("Lemon")
values.Add("Orange")
values.Add("Banana")
Fruits.DataSource = values
Fruits.DataBind()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Results.Text = Fruits.Items(Fruits.SelectedIndex).Text
Results.Text = Fruits.Value
End Sub
With the aspx:
<select id="Fruits" size="1" name="Fruits" runat="server"></select>
<asp:button id="Button1" runat="server" Text="Go"></asp:button>
<asp:label id="Results" runat="server">Results</asp:label>
Curiously, if I manually add the list items to the HtmlSelect control,
then the Value property correctly returns the selected menu item. For
example:
<select id="Fruits" size="1" name="Fruit2" runat="server">
<option selected>Apple</option>
<option>Lemon</option>
<option>Orange</option>
<option>Banana</option>
</select>
In both case, the resulting HTML is identical. Why does the DataBound
HtmlSelect fail to return the selected list item?
Thanks,
Stephen
consistently fails to return the item selected, defaulting instead to
the first item in the list. For example:
Protected WithEvents Fruits As System.Web.UI.HtmlControls.HtmlSelect
Protected WithEvents Results As System.Web.UI.WebControls.Label
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
…
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim values As ArrayList = New ArrayList()
values.Add("Apple")
values.Add("Lemon")
values.Add("Orange")
values.Add("Banana")
Fruits.DataSource = values
Fruits.DataBind()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Results.Text = Fruits.Items(Fruits.SelectedIndex).Text
Results.Text = Fruits.Value
End Sub
With the aspx:
<select id="Fruits" size="1" name="Fruits" runat="server"></select>
<asp:button id="Button1" runat="server" Text="Go"></asp:button>
<asp:label id="Results" runat="server">Results</asp:label>
Curiously, if I manually add the list items to the HtmlSelect control,
then the Value property correctly returns the selected menu item. For
example:
<select id="Fruits" size="1" name="Fruit2" runat="server">
<option selected>Apple</option>
<option>Lemon</option>
<option>Orange</option>
<option>Banana</option>
</select>
In both case, the resulting HTML is identical. Why does the DataBound
HtmlSelect fail to return the selected list item?
Thanks,
Stephen