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

当前回答

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

其他回答

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

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

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

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

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

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

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

对我有用的是将以下代码从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>");
}

这里没有提到的另一种方法是子类化ListBox

Ie.

public class ListBoxNoEventValidation : ListBox 
{
}

如果你子类化了system . web . ui . supportsevenvalidation属性,除非你显式地把它添加回来,否则它永远不会调用验证例程。这对任何控件都有效,而且是我发现的在一个控件的基础上“禁用”它的唯一方法(即,不是页面级别)。