By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

HtmlSelect populated dynamically, fails to return selected item

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jos
"Stephen Miller" <js******@hotmail.com> wrote in message
news:cd**************************@posting.google.c om...
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


Try to add the items dynamically in Page_Init instead of Page_Load.
The viewstate information (containing which item was selected) is added
before Page_Load, when your list is not yet filled.

--

Jos Branders
Nov 18 '05 #2

P: n/a
"Jos" <jo***************@fastmail.fm> wrote in message news:<ek**************@TK2MSFTNGP09.phx.gbl>...
"Stephen Miller" <js******@hotmail.com> wrote in message
news:cd**************************@posting.google.c om...
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


Try to add the items dynamically in Page_Init instead of Page_Load.
The viewstate information (containing which item was selected) is added
before Page_Load, when your list is not yet filled.


Jos,

That works, but is there a better way? I've got a lot of dependent
logic in the Page_Load method that I would rather not move or
replicate in Page_Init.

Interestingly, MSDN's sample code for HtmlSelect (see:
http://msdn.microsoft.com/library/de...ectcontrol.asp)
shows DataBinding in Page_Load and an onClick event accessing the
control's Value method.

Stephen
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.