473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HtmlSelect populated dynamically, fails to return selected item

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
2 5780
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jonathan | last post by:
Hi everyone, I have a problem with a HtmlSelect control that I am loading dynamically in javascript. Here is my code: <asp:DropDownList id="MyDropDownList1" onChange="LoadContent(this);"...
6
by: BK | last post by:
I'm having a really strange problem with and HtmlSelect server control (listbox) and a RequiredFieldValidator that I am trying to use to make sure that someone has items in the listbox. The way...
1
by: Suhail A, Salman | last post by:
Dear All, I placed a HtmlSelect control on a web page and set it to "Run at Server", the objective of this HtmlSelect control is that the client adds all his accounts to a text box, and...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
1
by: Ken | last post by:
I have an HTMLSelect server control that I am populating dynamically in the Page_Load event of my page. When I turn off VIEWSTATE, the HTMLSelect.Value property is blank when the page is posted...
5
by: David | last post by:
I want to get the Sel_142's multiple item's value, and put them in an array. if ( !Sel_142.Multiple ) vo.CA142 = Sel_142.Items.Value; else { Sel_142.Items..... }
3
by: Joe | last post by:
Hello All, I am populating an HtmlSelect control's Items property with four list items. The HtmlSelect control is contained in a user control which is dropped into a placeholder on the web form....
3
by: Lohboy | last post by:
Using ASP.NET and IE7. (Sorry if I am posting in the wrong forum but my problem seemed to be more related to the JavaScript side than the ASP.NET side.) I have two DropDownList controls the...
4
by: Ronny Mandal | last post by:
Hi! I have an .aspx with some controls that are created dynamically. The items are populated into the box by setting the DataSource-property to a list. In addition I specify the text and value...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.