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

当前回答

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

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

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

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

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

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

其他回答

如果您通过客户端脚本填写下拉列表,则在将表单提交回服务器之前清除列表;然后ASP。NET将不会抱怨,安全性将仍然存在。

为了从DDL中获得选择的数据,您可以将“OnChange”事件附加到DDL以收集隐藏输入或文本框中的值,并使用Style="display: none;"

我实现了一个嵌套的网格视图,我也遇到了同样的问题。我使用了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> 

对我有用的是将以下代码从page_load移动到page_prerender:

lstMain.DataBind();
Image img = (Image)lstMain.Items[0].FindControl("imgMain");

// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
    cs.RegisterStartupScript(cstype, csname1, "<script language=javascript> p=\"" + img.ClientID + "\"</script>");
}

我有同样的问题与Repeater,因为我有一个网页与一个网站的Repeater控件,已启用EnableEventValidation开关。这并不好。我得到无效的回发相关异常。

对我来说有用的是为Repeater设置EnableViewState="false"。它的优点是使用起来更简单,就像关闭网站或网页的事件验证一样简单,但是范围比关闭事件验证要小得多。

我通过没有将ListBox添加到父Page/Control控件集合来解决这个确切的错误。因为我真的不需要任何服务器端功能。我只是想用它来输出自定义服务器控件的HTML,这是我在OnRender事件处理程序中自己做的。我希望使用该控件可以使我不必编写自己的html响应。

这个解决方案可能不适合大多数人,但它使ASP。NET对控件执行ValidateEvent,因为控件在回发之间不会保留在内存中。

此外,我的错误是由选定的列表项是一个项目,不是在前回发的列表框。希望能帮到谁。