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

当前回答

(1) EnableEventValidation = " false "...................这对我没用。

(2) ClientScript.RegisterForEventValidation……这对我没用。

解决方案1:

在GridView中将Button/ImageButton更改为LinkButton。它的工作原理。(但我喜欢ImageButton)

研究:Button/ImageButton和LinkButton使用不同的回发方法

原文:

http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx

解决方案2:

在OnInit()中,输入如下代码来设置Button/ImageButton的唯一ID:

protected override void OnInit(EventArgs e) {
  foreach (GridViewRow grdRw in gvEvent.Rows) {

  Button deleteButton = (Button)grdRw.Cells[2].Controls[1];

  deleteButton.ID = "btnDelete_" + grdRw.RowIndex.ToString();           
  }
}

原文:

http://www.c-sharpcorner.com/Forums/Thread/35301/

其他回答

3:我改变了我的按钮类型在网格 列从“PushButton”到 “LinkButton”。它工作! ("ButtonType="LinkButton")我认为如果 你可以把你的按钮换成其他的 其他控件如“LinkButton” 在这种情况下,它会正常工作。

我希望我能给你投票,阿米尔(唉,我的代表太低了。)我只是有这个问题,改变这个工作就像一个冠军在我的网格视图。稍微提一下,我认为有效的代码是:ButtonType="Link"

我怀疑这是因为当你点击“编辑”时,你的编辑变成了“更新”和“取消”,然后在提交时又变成了“编辑”。这些变化的控件让。net很不舒服。

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

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

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

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

如果您预先知道可以填充的数据,那么可以使用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>标记中进行包装。

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