获取HTML下拉从code后面的列表值 [英] Getting HTML drop down list value from code behind

查看:109
本文介绍了获取HTML下拉从code后面的列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下拉列表和ASP按钮,在我的网页是这样的:

I have drop down list and asp button in my page like this:

<select id="select1" >
   <option value="smiley.gif">Smiley</option>
   <option value="angry.gif">Angry</option>
   <option value="stickman.gif">Stickman</option>
</select>
  <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />

在单击该按钮时,我需要从下拉列表中选择的值,并将其保存在会话中。
但是,当列表被改变了,它不应该重新加载页面(只要我不需要使用 =服务器)。
如何做到这一点?

Once the button is clicked, I need to get the selected value from the drop down list and save it in the session. But when the list is changed, it should not reload the page (Simply I don't need to use runat="server"). How to do this?

推荐答案

您获得从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;

这篇关于获取HTML下拉从code后面的列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆