且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取HTML下拉从code后面的列表值

更新时间:2023-11-12 13:15:16

您获得从Request对象的值。

You get the value from the Request object.

编辑,这要感谢@彼得注释,名称属性添加到选择元素:

Edit, thanks to comment by @Péter, add name property to the select element:

<select id="select1" name="select1">
   <option value="smiley.gif">Smiley</option>
   <option value="angry.gif">Angry</option>
   <option value="stickman.gif">Stickman</option>
</select>

在code-背后:

var selectedOption = Request["select1"];

不过,如果你正在使用asp.net我建议你使用选择的服务器版本(RUNAT =服务器),从而可在code-背后从选择的对象直接访问的值。

Though, if you are using asp.net I suggest you use the server version (runat=server) of the select and thereby could access the value directly from a Select object in code-behind.

要避免回发,您可以设置自动回=假,在选择下拉列表中的值时,下面的解决办法将不会回传。而你用你的下降一个asp.net控制下得到的类型安全等方面的好处:

To avoid postback you can set AutoPostback=false, the following solution would not post back when selecting a value in the drop down list. And you get the bonus of type safety etc by using an asp.net control for your drop down:

<asp:DropDownList id="select1" runat="server" AutoPostback="False">
   <asp:ListItem Value="smiley.gif" Text="Smiley"/>
   <asp:ListItem Value="angry.gif" Text="Angry"/>
   <asp:ListItem Value="stickman.gif" Text="Stickman"/>
</asp:DropDownList>
  <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />

在code-背后:

var selectedOption = select1.SelectedValue;