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

当前回答

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

其他回答

此错误将在没有回发的情况下显示

添加代码:

If(!IsPostBack){

 //do something

}

你需要做2或3个,不要禁用事件验证。

在asp:listbox客户端添加项目有两个主要问题。

The first is that it interferes with event validation. What came back to the server is not what it sent down. The second is that even if you disable event validation, when your page gets posted back the items in the listbox will be rebuilt from the viewstate, so any changes you made on the client are lost. The reason for this is that a asp.net does not expect the contents of a listbox to be modified on the client, it only expects a selection to be made, so it discards any changes you might have made.

最好的选择是像推荐的那样使用更新面板。另一个选择,如果你真的需要这样做的客户端,是使用普通的<select>而不是<asp:ListBox>,并保持你的项目列表在一个隐藏字段。当页面在客户端上呈现时,您可以从文本字段内容的分割中填充它。

然后,当您准备发布它时,您从修改后的<select>中重新填充隐藏字段的内容。然后,当然,您必须在服务器上再次分割它,并对您的项目做一些事情,因为您的选择现在是空的,因为它回到了服务器上。

总而言之,这是一个非常麻烦的解决方案,我并不推荐,但如果您确实需要对listBox进行客户端修改,它确实可以工作。但是,我真的建议您在走这条路之前查看一下updatePanel。

在客户端使用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);
}

正如Nick B说的,这对我很有用在某些情况下你必须删除换行符。看一下代码:

错误的方法:

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Selected="True">
            Item 1</asp:ListItem>
    <asp:ListItem>
            Item 2</asp:ListItem>
    <asp:ListItem>
            Item 3</asp:ListItem>
</asp:DropDownList>

-对道:

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Selected="True">Item 1</asp:ListItem>
    <asp:ListItem>Item 2</asp:ListItem>
    <asp:ListItem>Item 3</asp:ListItem>
</asp:DropDownList>

我只在IE10+中遇到过

The following example shows how to test the value of the IsPostBack property when the page is loaded in order to determine whether the page is being rendered for the first time or is responding to a postback. If the page is being rendered for the first time, the code calls the Page.Validate method. The page markup (not shown) contains RequiredFieldValidator controls that display asterisks if no entry is made for a required input field. Calling Page.Validate causes the asterisks to be displayed immediately when the page is rendered, instead of waiting until the user clicks the Submit button. After a postback, you do not have to call Page.Validate, because that method is called as part of the Page life cycle.

 private void Page_Load()
    {
        if (!IsPostBack)
        {      
        }
    }