“无效的回发或回调参数"带有数据绑定控件 [英] "Invalid postback or callback argument" with Databound controls

查看:24
本文介绍了“无效的回发或回调参数"带有数据绑定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的数据绑定控件上发生事件(添加/编辑/删除)时,我收到以下错误.

I am getting the following error when an event (Add/Edit/Delete) occurs on my databound control.

无效的回发或回调参数.使用 in 启用事件验证配置或 <%@ 页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.

我使用的是自定义 DataList 控件,但 GridView、DetailsView、FormView 和 Repeater 控件(可能还有其他数据绑定控件)也会出现此问题.

I am using a custom DataList control, but this problem also occurs with GridView, DetailsView, FormView and Repeater control (and maybe with other databound controls).

我能找到的答案告诉我关闭配置文件或页面中的验证,但这听起来并不是最好的解决方案.我做错了什么?

The answers I can find tell me to turn off the validation in the config file or page, but that does not sound like it is the best solution. What am I doing wrong?

推荐答案

问题是在页面Load事件中加载控件的数据,调用DataBind()方法.但是,如果在引发事件之前调用 DataBind() 方法,则由于控件命名已更改,则会生成上述异常.

The problem is loading the data for the control in the page Load event and calling the DataBind() method. However it appears that if the DataBind() method is called before the events are raised the above exception is generated as the control naming has changed.

解决方案是将其更改为 if(!IsPostback) DataBind(),然后在事件处理程序的末尾调用 DataBind() 方法.无论如何,您大部分时间都需要在处理程序结束时调用它以影响更改.

The solution is to change this to if(!IsPostback) DataBind() and then call the DataBind() method at the end of the event handler. You would need to call it most of the time anyway at the end of the handler to affect the changes.

如果这不是您的问题,并且您正在使用 JavaScript 修改控件客户端,请查看 这篇文章.

If this is not your problem, and you are modifying controls client side using JavaScript, check out this article.

这是一篇自我回答的帖子,因为我在我的博客上收到了很多关于这个问题的回复,并认为我可以进一步分享.

这篇关于“无效的回发或回调参数"带有数据绑定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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