当我从客户端返回一个页面时,我得到以下错误。我有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

当前回答

在将常规ASPX页面转换为Content页面时,我们也遇到了同样的问题。

有此问题的页面在其中一个Content部分内有一个</form>标记,因此在运行时呈现了两个表单结束标记,从而导致了此问题。从页面中删除额外的表单结束标记解决了此问题。

其他回答

如果您预先知道可以填充的数据,那么可以使用ClientScriptManager来解决这个问题。当我在以前的用户选择上使用javascript动态填充下拉框时,我遇到了这个问题。

下面是一些覆盖呈现方法(在VB和c#中)并为下拉列表ddCar声明潜在值的示例代码。

在VB:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    Dim ClientScript As ClientScriptManager = Page.ClientScript

    ClientScript.RegisterForEventValidation("ddCar", "Mercedes")

    MyBase.Render(writer)
End Sub

或者c#的轻微变化可以是:

protected override void Render(HtmlTextWriter writer)
{
    Page.ClientScript.RegisterForEventValidation("ddCar", "Mercedes");
    base.Render(writer);
}

对于新手:这应该在文件(.vb或.cs)后面的代码中,或者如果在aspx文件中使用,您可以在<script>标记中进行包装。

在将常规ASPX页面转换为Content页面时,我们也遇到了同样的问题。

有此问题的页面在其中一个Content部分内有一个</form>标记,因此在运行时呈现了两个表单结束标记,从而导致了此问题。从页面中删除额外的表单结束标记解决了此问题。

如果您正在使用gridview,而不是在pageload inside !ispostback中绑定gridview,那么当您单击gridview中的编辑和删除行时,将出现此错误。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        bindGridview();
        }

我也遇到过同样的问题

刚刚添加了一个条件if(!IsPostBack),它工作得很好:)

当我在ItemDataBound上添加id时,它没有给我错误,但它没有给我命令名。它返回命令名为空。然后我添加了命令名以及ItemDataBound。然后它解决了同样的问题。谢谢Nilesh,好建议。它起作用了:)