使用 JavaScript 重新排列的 ListBox 元素导致回发时发生事件验证错误

ListBox elements rearranged with JavaScript causing event validation error on postback(使用 JavaScript 重新排列的 ListBox 元素导致回发时发生事件验证错误)
本文介绍了使用 JavaScript 重新排列的 ListBox 元素导致回发时发生事件验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含两个列表框和一些按钮的项目交换器控件,这些按钮允许我在两个列表之间交换项目.交换是使用javascript完成的.我还在列表中上下移动项目.基本上,当我将项目移动到右侧的列表框时,我将元素(GUID)的数据键存储在隐藏字段中.在回发时,我只是从现场读取 GUID.一切正常,但在回发时,我得到以下异常:

I have created an item swapper control consisting in two listboxes and some buttons that allow me to swap items between the two lists. The swapping is done using javascript. I also move items up and down in the list. Basically when I move the items to the list box on the right I store the datakeys of the elements (GUIDs) in a hiddenfield. On postback I simply read the GUIDs from the field. Everything works great but on postback, I get the following exception:

回发或回调参数无效.使用配置中或页面中的 <%@ Page EnableEventValidation="true" %> 启用事件验证.出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件.如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证.

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

我已经准备了一个测试应用程序.您所要做的就是下载存档并运行项目.在网页上选择 3 个项目,按全部添加,然后将第三个元素上移一级,然后点击按钮".错误会出现.关闭事件验证是绝对不能接受的.谁能帮帮我,我已经花了两天时间没有找到解决方案.

I've prepared a test application. All you have to do is download the archive and run the project. On the web page select the 3 items, press Add all, then move the third element up one level and then hit "Button". The error will show up. Turning event validation off is by no means acceptable. Can anyone help me, I've spent two already days without finding a solution.

测试申请

推荐答案

第一个选项会带来相当大的开销.我已经定义了我自己的从列表框类派生的自定义列表框控件,并执行了 loadpostback 数据的覆盖:

The first option will bring considerable overhead. I have defined my own custom listbox control derived from the listbox class and performed an override of the loadpostback data:

public class CustomListBox : ListBox
{
    protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
    {
        return true;
    }
}

在我的用户控件中使用它而不是常规列表框解决了问题,但是我的方法是否存在任何风险?

Using this instead of the regular listbox in my user control solved the problem, however are there any risks associated with my approach?

这篇关于使用 JavaScript 重新排列的 ListBox 元素导致回发时发生事件验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)