[英]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.