[英]Asp.Net: Restoring client-side SelectedItem of DropDownList on server-side
So I have a dropDownList on my page that contains hundreds of items. 因此,我的页面上有一个dropDownList,其中包含数百个项目。 The user can filter this DDL by typing some text into a textbox.
用户可以通过在文本框中键入一些文本来过滤此DDL。 The DDL then gets filtered accordingly (all items that do not contain the entered text are removed via JavaScript).
然后对DDL进行相应的过滤(所有不包含输入文本的项目都将通过JavaScript删除)。 The user then selects his item and presses a button.
然后,用户选择他的项目并按下按钮。 Usually, this would cause an error because the DDL has been altered and ASP validates the PostBack data.
通常,这将导致错误,因为DDL已被更改并且ASP验证了PostBack数据。 However, with
EnableEventValidation="false"
you can turn off this behavior and the page gets submited properly. 但是,使用
EnableEventValidation="false"
可以关闭此行为,并且可以正确EnableEventValidation="false"
页面。 But (and thats my problem): the SelectedIndex of the DDL is always "0" on server-side and thus the SelectedItem is the wrong one. 但是(那是我的问题):DDL的SelectedIndex在服务器端始终为“ 0”,因此SelectedItem是错误的。 So obviously, the changes on client-side are dismissed.
所以很明显,客户端的更改被忽略了。 Does anybody have an idea on how to get the correct SelectedItem?
有人对如何获取正确的SelectedItem有想法吗? Or a better way to filter a DDL and maintain the correct SelectedItem?
还是一种更好的方法来过滤DDL并维护正确的SelectedItem?
When user presses a button get current value of dropdown using jQuery and set it in hidden field on page , give hidden field runat="server" so that when it posts back you will get value that was selected. 当用户按下按钮时,使用jQuery获取下拉列表的当前值并将其设置在页面的隐藏字段中,将隐藏字段设置为runat =“ server”,以便在其回发时将获得选定的值。 For example
例如
<asp:DropDownList class="myList"></asp:DropDownList>
<asp:Button class="btn"/>
<input type="hidden" id="hdnSelectedI" runat="server" class="hiddenControl">
$(document).ready(function(){
$(".btn").click(function(){
var selectedItem = $(".myList").val();
$(".hiddenControl").val(selectedItem);
});
});
I have used clas name selector as ids in aspnet are auto generated. 我已经使用clas名称选择器,因为aspnet中的ID是自动生成的。 On server side get value of
hdnSelectedItem.Value
, and from that pull from list of items/db maintained on server. 在服务器端,获取
hdnSelectedItem.Value
值,然后从服务器上维护的项目/数据库列表中提取该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.