Asp.Net:DropDownList中的服务器端还原客户端的SelectedItem [英] Asp.Net: Restoring client-side SelectedItem of DropDownList on server-side

查看:161
本文介绍了Asp.Net:DropDownList中的服务器端还原客户端的SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我的网页上一个DropDownList包含几百个项目。用户可以通过输入一些文字到一个文本框筛选该DDL。该DDL然后获取相应的过滤(不包含输入的文本中的所有项目都通过JavaScript删除)。然后,用户选择他的产品和presses一个按钮。通常情况下,因为DDL已经改变,这将导致错误和ASP验证回发数据。然而,随着 EnableEventValidation =FALSE
您可以关闭此行为,并在页面才能正确submited。但是,(这就是我的问题):在DDL的selectedIndex始终为0的服务器端,因而则selectedItem是错误的。所以,很显然,在客户端的变化被解雇。是否有人对如何得到正确的SelectedItem的想法?或者更好的方式来筛选DDL和保持正确的SelectedItem?

So I have a dropDownList on my page that contains hundreds of items. The user can filter this DDL by typing some text into a textbox. The DDL then gets filtered accordingly (all items that do not contain the entered text are removed via 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. However, with EnableEventValidation="false" you can turn off this behavior and the page gets submited properly. But (and thats my problem): the SelectedIndex of the DDL is always "0" on server-side and thus the SelectedItem is the wrong one. So obviously, the changes on client-side are dismissed. Does anybody have an idea on how to get the correct SelectedItem? Or a better way to filter a DDL and maintain the correct SelectedItem?

推荐答案

当用户presses使用jQuery按钮获取下拉列表的当前值,并将其设置在隐藏字段页面上,给隐藏字段=服务器,这样当它回发,你会得到选择该值。
例如:

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. 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);  

    });

});

我用CLAS名选择在ASPNET ID是自动生成的。在服务器端获取的价值
hdnSelectedItem.Value ,并从项目列表中拉/ DB维护服务器。

I have used clas name selector as ids in aspnet are auto generated. On server side get value of hdnSelectedItem.Value , and from that pull from list of items/db maintained on server.

这篇关于Asp.Net:DropDownList中的服务器端还原客户端的SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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