简体   繁体   English

Asp.Net:在服务器端还原DropDownList的客户端SelectedItem

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

相关问题 ASP.NET双列表框使用JQuery更新了客户端,以及如何在服务器端检索结果 - ASP.NET dual listboxes updated client-side with JQuery, and how to retrieve the results server-side 用于客户端和服务器端验证的 ASP.NET Core 重用代码 - ASP.NET Core Reuse Code for Client-Side and Server-Side Validation 如何从客户端调用服务器端ASP.NET事件 - how to call a server-side ASP.NET event from the client-side ASP.NET:客户端验证,服务器端操作 - ASP.NET: Client-side validation, server-side action ASP.NET客户端与服务器端验证 - ASP.NET Client-side vs Server-side Validation 从服务器端asp.net注册客户端json的有效方法? - Efficient way to register client-side json from server-side asp.net? 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? 如何在ASP.NET中从客户端调用服务器端函数? - How to call server-side function from client-side in ASP.NET? 客户端(jquery)和服务器端(asp.net)应用程序都可以访问的服务 - Service that can be accessed by both client-side (jquery) and server-side (asp.net) applications 结合 Asp.Net Core 服务器端和 React 客户端 - Combining Asp.Net Core server-side and React client-side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM