如何在html选择元素中获取所选项目的文本?

时间:2017-03-07 17:53:24

标签: asp.net vb.net html-select

我需要html select元素中所选项的值。 Here接受的答案建议这样做:

Request.Form["testSelect"]

所以在我的情况下,我有这个html选择元素:

<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>

然而,设计时编译器并不满意,正如#34; Request.Form&#34;下面的曲线所示。和&#34;子类别&#34;的白度:

enter image description here

从VB.NET中获取该值需要做什么?

更新

当我尝试Sailor在他的回答中提出的建议时(添加&#34; runat =&#34;服务器&#34;&#34;到html选择元素),我得到了:

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

删除它会使错误消失。

更新2

此:

Subcategory = Request.Form.Item("selectSubcategory")

...给我所选项目的索引(例如&#34; 0&#34;);我怎样才能得到实际的文字?

更新3

事实证明,它并没有真正给我所选项目的索引,毕竟它只是给了我&#34; 0&#34; always - 是否选择了第一个(第0个)或第二个(第一个)选项。

如果我尝试开头提到的内容,请使用这种代码:

Subcategory = Request.Form["selectSubcategory"]

我收到此错误:

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

这就是我走另一条路的原因:

Subcategory = Request.Form.Item("selectSubcategory").ToString()

......但是,这并没有完全削减芥末(只是总是返回0)。

1 个答案:

答案 0 :(得分:3)

确保您的表单标记中包含runat="server"。否则将其保留在Select标签中。

然后尝试使用

Request.Form.Item("Subcategory")