Html 如何使用asp.net获取html选择的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2378338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:24:13  来源:igfitidea点击:

How to get selected value of a html select with asp.net

asp.nethtmlhtml-select

提问by HasanG

I have code below:

我有以下代码:

<select id="testSelect">
    <option value="1">One</option>
    <option value="2">Two</option>
</select>
<asp:Button ID="btnTest" runat="server" Text="Test it!" onclick="btnTest_Click" />

I need to get selected options' value on postback. How can I do this with asp.net?

我需要在回发时获取所选选项的值。我怎样才能用 asp.net 做到这一点?

回答by Kobi

You need to add a name to your <select>element:

您需要为<select>元素添加一个名称:

<select id="testSelect" name="testSelect">

It will be posted to the server, and you can see it using:

它将被发布到服务器,您可以使用以下命令查看它:

Request.Form["testSelect"]

回答by Paulo Henrique Queiroz

I've used this solution to get what you need.

我已经使用这个解决方案来获得你需要的东西。

Let'say that in my .aspx code there's a select list runat="server":

假设在我的 .aspx 代码中有一个选择列表 runat="server":

<select id="testSelect"  runat="server" ClientIDMode="Static" required>
    <option value="1">One</option>
    <option value="2">Two</option>
</select>

In my C# code I used the code below to retrieve the text and also value of the options:

在我的 C# 代码中,我使用下面的代码来检索文本和选项的值:

testSelect.SelectedIndex == 0 ? "uninformed" : 
    testSelect.Items[testSelect.SelectedIndex].Text);

In this case I check if the user selected any of the options. If there's nothing selected I show the text as "uninformed".

在这种情况下,我检查用户是否选择了任何选项。如果没有选择任何内容,我会将文本显示为“不知情”。

回答by Pontus Bremdahl

If you would use asp:dropdownlistyou could select it easier by testSelect.Text.

如果您要使用,asp:dropdownlist您可以通过 更轻松地选择它testSelect.Text

Now you'd have to do a Request.Form["testSelect"]to get the value after pressed btnTes.

现在,您必须执行 aRequest.Form["testSelect"]才能在按下后获取值btnTes

Hope it helps.

希望能帮助到你。

EDIT: You need to specify a nameof the select (not only ID) to be able to Request.Form["testSelect"]

编辑:您需要指定一个name选择(不仅是 ID)才能Request.Form["testSelect"]

回答by QuanCSE

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">    
<head>
    <title> HtmlSelect Example </title>
    <script runat="server">
      void Button_Click (Object sender, EventArgs e)
      {
         Label1.Text = "Selected index: " + Select1.SelectedIndex.ToString()
                       + ", value: " + Select1.Value;    
      }    
   </script>    
</head>    
<body>    
   <form id="form1" runat="server">

      Select an item: 

      <select id="Select1" runat="server">    
         <option value="Text for Item 1" selected="selected"> Item 1 </option>
         <option value="Text for Item 2"> Item 2 </option>
         <option value="Text for Item 3"> Item 3 </option>
         <option value="Text for Item 4"> Item 4 </option>
      </select>

      <button onserverclick="Button_Click" runat="server" Text="Submit"/>

      <asp:Label id="Label1" runat="server"/>    
   </form>
</body>
</html>

Source from Microsoft. Hope this is helpful!

来源来自微软。希望这是有帮助的!

回答by Pavunkumar

Java script:

Java脚本:

use elementid. selectedIndex()function to get the selected index

使用 elementid. selectedIndex()函数获取所选索引