HtmlSelect populated dynamically, fails to return selected item

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
 
J

Jos

Stephen Miller said:
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.
 
S

Stephen Miller

Jos said:
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/d...n-us/cpgenref/html/cpconhtmlselectcontrol.asp)
shows DataBinding in Page_Load and an onClick event accessing the
control's Value method.

Stephen
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top