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

当前回答

我实现了一个嵌套的网格视图,我也遇到了同样的问题。我使用了LinkButton而不是像这样的图像按钮:

在我有这样的专栏之前:

<asp:TemplateField ItemStyle-Width="9">
  <ItemTemplate>
 <asp:ImageButton ID="ImgBtn" ImageUrl="Include/images/gridplus.gif" CommandName="Expand"
                        runat="server" />
  </ItemTemplate>
</asp:TemplateField>

我像这样替换了。

<asp:TemplateField>
<ItemTemplate>
     <asp:LinkButton  CommandName="Expand" ID="lnkBtn"  runat="server" ><asp:Image  ID="Img"  runat="server" ImageUrl="~/Images/app/plus.gif" /></asp:LinkButton>
      </ItemTemplate>
</asp:TemplateField> 

其他回答

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

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

在Page_Load事件中有代码吗?如果是,那么添加以下内容可能会有所帮助。

if (!Page.IsPostBack)
{ //do something }

在正常的生命周期中,当您单击命令并再次运行Page_load时,将抛出此错误 Page_Load ->单击“Command -> Page_Load(再次)-> Process ItemCommand Event”

这就是我得到它的原因:

我有一个ASP:ListBox。最初它是隐藏的。在客户端,我将通过AJAX填充它的选项。用户选择了一个选项。然后,当单击Submit按钮时,服务器会对ListBox感到奇怪,因为它不记得它有任何选项。

因此,我所做的是确保在将表单提交回服务器之前清除所有列表选项。这样服务器就不会抱怨,因为列表到客户端时是空的,返回时也是空的。

排序! !

正如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+中遇到过