当我从客户端返回一个页面时,我得到以下错误。我有JavaScript代码,修改asp:ListBox在客户端。

我们如何解决这个问题?

错误详情如下:

Server Error in '/XXX' Application.

--------------------------------------------------------------------------------
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.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[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.]
   System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +2132728
   System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +108
   System.Web.UI.WebControls.ListBox.LoadPostData(String postDataKey, NameValueCollection postCollection) +274
   System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +11
   System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +353
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1194

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433

当前回答

如果您使用Ajax更新面板。添加<Triggers>标签,并在其中使用<asp:PostBackTrigger…/>触发按钮或控件导致回发

其他回答

我的问题解决时取消事件结束时网格事件在服务器端。

protected void grdEducation_RowEditing(object sender, GridViewEditEventArgs e)
{
  // do your processing ...

  // at end<br />
  e.Cancel = true;
}

我遇到了同样的问题,两个列表框和两个按钮。

列表框中的数据是从数据库中加载的,您可以通过单击按钮在框之间移动项目。

我收到了一个无效的回发。

结果是数据中有回车换行符,当显示在列表框中时无法看到。

在除ie10和ie11之外的所有浏览器中都运行良好。

拆下车架回换线,一切正常。

对我们来说,问题只在生产环境中随机发生。RegisterForEventValidation没有为我们做任何事情。

最后,我们发现在asp.net应用程序运行的web场中,两台IIS服务器安装了不同的。net版本。所以看起来他们有不同的规则加密asp.net验证散列。更新它们解决了大部分问题。

另外,我们在web中配置了machineKey(compatibilityMode)(在两个服务器中相同),httpRuntime(targetFramework), ValidationSettings:UnobtrusiveValidationMode, pages(renderAllHiddenFieldsAtTopOfForm)。配置两个服务器。

我们使用该站点生成密钥https://www.allkeysgenerator.com/Random/ASP-Net-MachineKey-Generator.aspx

我们花了很多时间来解决这个问题,我希望这对大家有所帮助。

<appSettings>
   <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
...
</appSettings>
<system.web>
   <machineKey compatibilityMode="Framework45" decryptionKey="somekey" validationKey="otherkey" validation="SHA1" decryption="AES />
   <pages [...] controlRenderingCompatibilityVersion="4.0" enableEventValidation="true" renderAllHiddenFieldsAtTopOfForm="true" />
   <httpRuntime [...] requestValidationMode="2.0" targetFramework="4.5" />
...
</system.web>

检查绑定控件的数据。一些无效数据破坏了ValidateEvent。

在客户端使用JavaScript修改ListBox时,我也遇到了同样的问题。当您从客户端向ListBox添加页面呈现时不存在的新项时,就会发生这种情况。

我找到的修复方法是通知事件验证系统可以从客户端添加所有可能的有效项。您可以通过重写Page来实现这一点。渲染并调用Page.ClientScript.RegisterForEventValidation为你的JavaScript可以添加到列表框的每个值:

protected override void Render(HtmlTextWriter writer)
{
    foreach (string val in allPossibleListBoxValues)
    {
        Page.ClientScript.RegisterForEventValidation(myListBox.UniqueID, val);
    }
    base.Render(writer);
}

如果列表框有大量潜在的有效值,这可能有点麻烦。在我的例子中,我在两个listbox之间移动项目——一个有所有可能的值,另一个最初是空的,但当用户单击按钮时,在JavaScript中用第一个值的子集填充。在这种情况下,你只需要遍历第一个列表框中的项目,并将每个项目注册到第二个列表框中:

protected override void Render(HtmlTextWriter writer)
{
    foreach (ListItem i in listBoxAll.Items)
    {
        Page.ClientScript.RegisterForEventValidation(listBoxSelected.UniqueID, i.Value);
    }
    base.Render(writer);
}