简体   繁体   English

从asp.net代码后面检索HtmlSelect选项值

[英]Retrieve HtmlSelect option values from asp.net code behind

I have a HTML select control in ASP.Net WebForm application. 我在ASP.Net WebForm应用程序中有一个HTML select控件。 Its option values are added from jquery in client side. 它的选项值是从客户端的jquery添加的。 I would like to read those values from server side when the page is posted back. 当页面回发时,我想从服务器端读取这些值。 I have the following code, but it does not work. 我有以下代码,但是它不起作用。 Option values added from client side are not available in code behind. 从客户端添加的选项值在后面的代码中不可用。 How can i make those values available to code behind? 如何使这些值可用于后面的代码?

//Client code
<select title="Select one" id="selectBooks" multiple="multiple">
     <option value="1">test</option> //added in client side.
</select>

//Code behind
System.Web.UI.HtmlControls.HtmlSelect books= (System.Web.UI.HtmlControls.HtmlSelect)form1.FindControl("selectBooks");

foreach (ListItem item in books.Items)
{
   string test = item.Text.ToString();
}

ASP code: ASP代码:

Request["selectedBooks"]

Hopefully this will work. 希望这会起作用。

Your server can only know of data that is being submited to it. 您的服务器只能知道正在提交给它的数据。
Adding an option to a select control will just add it client side. 向选择控件添加选项只会在客户端添加它。
The value will be submitted if the users selects it but otherwise your server will never receive that data. 如果用户选择该值,则将提交该值,否则服务器将永远不会接收该数据。
If you must have the extra values you have to make sure that they reach the server one way or another. 如果必须具有额外的值,则必须确保它们以一种或另一种方式到达服务器。

The first idea that pops to my mind is adding the values to a hidden input field as well as the select control so that when the user does a postback you can read the values from the inputfield. 我想到的第一个想法是将值添加到隐藏的输入字段以及选择控件中,以便用户执行回发时可以从输入字段读取值。
Something like this : 像这样的东西:

//Client code
<input id="selectionData" runat="server" type="hidden" value="2--addedvalue1;3--addedvalue2"/>
<select title="Select one" id="selectBooks" multiple="multiple">
     <option value="1">test</option>
     <option value="2">addedvalue1</option> //added in client side.
     <option value="3">addedvalue2</option> //added in client side.
</select>

//Server side
var data = selectionData.Value;
//add code to parse the data

Here i suggest you to keep a hidden field on the page where you HTML select control is 在这里,我建议您在HTML选择控件所在的页面上保留一个隐藏字段

then on clientclick event of some button set all selected values of HTML select in this hidden field. 然后在某个按钮的clientclick事件上设置HTML的所有选定值,然后在此隐藏字段中进行选择 like HdnValues.value="1,2,3,4"; 像HdnValues.value =“ 1,2,3,4”;

now on post back you can find the hidden field value on server. 现在回发,您可以在服务器上找到隐藏字段值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文件asp.net后面的代码中检索文本框值 - Retrieve textbox values in code behind file asp.net 将值从GridView传递到ASP.NET中文件后面的代码 - Passing values from GridView to code behind file in ASP.NET TELERIK / ASP.NET-无法从后面的代码中检索文本框文本 - TELERIK/ASP.NET - Cannot retrieve textbox text, from code behind ASP.net C# 读取文字控件<select><Option>从后面的代码 - ASP.net C# Read LiteralControl <select> <Option> from code behind 如何从C#中ASP.NET中的背后代码以多选模式检索绑定的列表框? - How to retrieve binded listbox in multiple selection mode from code behind in ASP.NET in C#? 在ASP.NET中使用onChange从HTMLSelect的指定值加载数据 - Load data from specified value from HTMLSelect with onChange in ASP.NET 尝试在ASP.NET中的ListView中的代码后面的文本框中分配值 - Attempting to assign values to TextBoxes in code behind in ListView in ASP.NET jQuery窗口值不能在后面的asp.net代码中访问 - jquery window values are not accessible in asp.net code behind 使用数据绑定在ASP.NET中使用optgroup元素填充HtmlSelect - Using databinding to populate a HtmlSelect with optgroup elements in asp.net 在asp.net后面的代码中从另一个页面返回值的最佳方法是什么? - What is the best way to return values from another page in code behind in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM