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

当前回答

最好的选择是使用隐藏字段,不禁用事件验证,也改变每个列表框,下拉列表选择与runat服务器属性

其他回答

如果你把UseSubmitBehavior="True"改成UseSubmitBehavior="False",你的问题就解决了

<asp:Button ID="BtnDis" runat="server" CommandName="BtnDis" CommandArgument='<%#Eval("Id")%>' Text="Discription" CausesValidation="True" UseSubmitBehavior="False" />

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

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

问题是ASP。NET不会知道这个额外的或已删除的列表项。 你有很多选择(如下所示):

禁用eventvalidation(这是个坏主意,因为你损失了一点安全性,而代价却很小)。 使用ASP。NET Ajax UpdatePanel。(如果你添加或删除列表框,将列表框放在更新面板中并触发更新。通过这种方式,viewstate和相关字段得到更新,eventvalidation将通过。) 忘记客户端,使用经典的回发并添加或删除listitems服务器端。

Ajax UpdatePanel可以做到这一点,我认为这是最简单的方法,忽略Ajax回发开销。

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

添加代码:

If(!IsPostBack){

 //do something

}