如何阻止 ASP.Net 网站在向前/向后浏览项目分页列表时抛出 HttpUnhandledException 异常?
我有一个 ASP.Net 托管网站,它将结果列表显示为带有结果分页的 DataGrid 或 ASP.Net Repeater。
如果通过按“上一个”/“下一个”选项卡快速滚动页面,有时会抛出 HttpUnhandledException 并呈现调试页面,而不是下一个结果列表。
调试屏幕如下:
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> 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.
at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument)
at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
--- End of inner exception stack trace ---
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.contacts_default_aspx.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
我在以下位置找到了对此类错误的引用 http://blogs.msdn.com/amitsh/archive/2007/07/31/why-i-get-invalid-postback-or-callback-argument-errors.aspx
但建议的解决方案是设置为 false,但这似乎会造成安全漏洞。 这些评论推荐了几种替代方案,但似乎都相当复杂,因为它们需要向我的站点中使用的每个 DataGrid 或 Repeater 控件添加代码。
是否有更通用的解决方案可以在不牺牲安全性的情况下完成?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回发时不会提供页面上的隐藏字段。
其中有几个字段,它们通常是 ASP.Net 提供的“魔力”所必需的。
在页面指令中,您可以将enableEventValidation = false放入页面指令中以将其关闭,但是这可能并不理想。
您可以移动写入将元素移动到页面顶部的代码
最后,我相当确定这个确切的问题最近已在 Service Pack 或 3.5 中得到修复。
-- 编辑 --
我刚刚找到了设置: RenderAllHiddenFieldsAtTopOfForm
根据MSDN,以下版本支持:3.5 SP1、3.0 SP2、2.0 SP2
The hidden fields on your page are not being supplied on postback.
There are several of these fields, and they are usually required for the 'magic' that ASP.Net provides.
In your page directive you can put enableEventValidation=false in your page directive to turn it off, however that may not be desirable.
You can move the write code to move the elements to the top of the page
Finally, I am fairly certain this exact issue was recently fixed in a Service Pack or in 3.5.
-- EDIT --
I just found the setting: RenderAllHiddenFieldsAtTopOfForm
According to MSDN is supported in the following versions: 3.5 SP1, 3.0 SP2, 2.0 SP2