I need the value of the selected item in an html select element. Here the accepted answer recommends doing it like so:
Request.Form["testSelect"]
So in my case, I've got this html select element:
<select name="subcategory" color="<%=Session("TextColor")%>" style="font: 8pt arial" onchange="UpdateFlag=true;">
<% if Not IsNewBusiness then %>
<option <% If subcategory = "0" Then Response.Write(" selected")%> value="0">Existing
<option <% If subcategory = "1" Then Response.Write(" selected")%> value="1">Organic Growth
<% else %>
<option <% If subcategory = "0" Then Response.Write(" selected")%> value="0">New
<option <% If subcategory = "1" Then Response.Write(" selected")%> value="1">Assumed
<% end if %>
</select>
Yet the design-time compiler is not happy with it, as can be seen by the squiggles beneath "Request.Form" and the whiteness of "subcategory":
What do I need to do to grab that value from VB.NET?
UPDATE
When I tried what Sailor suggested in his answer (added "runat="server"" to the html select element), I got:
Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following
specific parse error details and modify your source file appropriately.
Parser Error Message: Server tags cannot contain <% ... %> constructs.
Source Error:
Line 744: </td>
Line 745: <td nowrap align="left" valign="top">
Line 746: <select runat="server" name="subcategory" color="<%=Session
("TextColor")%>" style="font: 8pt arial" onchange="UpdateFlag=true;">
Line 747: <% If Not IsNewBusiness Then%>
Line 748: <option <% If Subcategory = "0" Then Response.Write(" selected")%>
value="0">
Source File: /EMS/customerreportingnet/pages/custmaint_entry.aspx Line: 746
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491
Removing it obliterated the err.
UPDATE 2
This:
Subcategory = Request.Form.Item("selectSubcategory")
...is giving me the index of the selected item (such as "0"); how can I get the actual text instead?
UPDATE 3
It turns out that it's not really giving me the index of the selected item, after all - it is simply giving me "0" always - whether the first (0th) or second (1st) option is selected.
If I try what is mentioned at the outset, using this kind of code:
Subcategory = Request.Form["selectSubcategory"]
I get this error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30311: Value of type 'System.Collections.Specialized.NameValueCollection' cannot be converted to 'String'.
Source Error:
Line 90: CustNo = Request.Form.Item("CustNo")
Line 91: 'Subcategory = Request.Form.Item("selectSubcategory").ToString() '<= question at https://stackoverflow.com/questions/42655000/how-can-i-reference-the-selected-item-in-an-html-select-element-vb-net
Line 92: Subcategory = Request.Form["selectSubcategory"]
Line 93: AutoID = Request.Form.Item("AutoID")
Line 94: ReturnMsg = ""
Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx Line: 92
That's why I went the other route, with:
Subcategory = Request.Form.Item("selectSubcategory").ToString()
...but that's not quite cutting the mustard, either (just returning 0 always).
Make sure you have
runat="server"
in your form tag. Otherwise keep it in the Select tag.And then try using