无效的回发或回调参数。使用'启用事件验证' “

问题描述 投票:228回答:39

当我从客户端发回页面时,我收到以下错误。我有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
.net asp.net .net-2.0 postback argumentexception
39个回答
169
投票

问题是ASP.NET无法了解这个额外的或删除的listitem。你有很多选择(如下所列):

  • 禁用事件验证(糟糕的想法,因为你失去了一点安全性,成本很低)。
  • 使用ASP.NET Ajax UpdatePanel。 (如果添加或删除列表框,请将列表框放在Updatepanel中并触发更新。这样,viewstate和相关字段将获得更新,并且事件验证将通过。)
  • 忘记客户端并使用经典回发并添加或删除listitems服务器端。

我希望这有帮助。


7
投票

(1)EnableEventValidation =“false”...................它对我不起作用。

(2)ClientScript.RegisterForEventValidation ....它对我不起作用。

解决方案1:

在GridView中将Button / ImageButton更改为LinkBut​​ton。有用。 (但我喜欢ImageButton)

研究:Button / ImageButton和LinkBut​​ton使用不同的方法进行回发

来源文章:

http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx

解决方案2:

在OnInit()中,输入类似这样的代码来设置Button / ImageButton的唯一ID:

protected override void OnInit(EventArgs e) {
  foreach (GridViewRow grdRw in gvEvent.Rows) {

  Button deleteButton = (Button)grdRw.Cells[2].Controls[1];

  deleteButton.ID = "btnDelete_" + grdRw.RowIndex.ToString();           
  }
}

来源文章:

http://www.c-sharpcorner.com/Forums/Thread/35301/


7
投票

你在.aspx页面尝试类似的东西

EnableEventValidation = “假”

你可以随意提出任何问题!


6
投票

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

要获取从DDL中选择的数据,可以将“OnChange”事件附加到DDL,以使用Style =“display:none;”在隐藏的Input或文本框中收集值。


6
投票

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

在我有这样一个列之前:

<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> 

5
投票

我有类似的问题,但我没有使用ASP.Net 1.1,也没有通过javascript更新控件。我的问题只发生在Firefox而不是IE(!)上。

我在PreRender事件上向DropDownList添加了选项,如下所示:

DropDownList DD = (DropDownList)F.FindControl("DDlista");
HiddenField HF = (HiddenField)F.FindControl("HFlista");
string[] opcoes = HF.value.Split('\n');
foreach (string opcao in opcoes) DD.Items.Add(opcao);

我的“GF”(隐藏字段)具有由换行符分隔的选项,如下所示:

HF.value = "option 1\n\roption 2\n\roption 3";

问题是HTML页面在代表DropDown的“select”选项上被打破了(我的意思是换行)。

所以我解决了我的问题,添加了一行:

DropDownList DD = (DropDownList)F.FindControl("DDlista");
HiddenField HF = (HiddenField)F.FindControl("HFlista");
string dados = HF.Value.Replace("\r", "");
string[] opcoes = dados.Split('\n');
foreach (string opcao in opcoes) DD.Items.Add(opcao);

希望这能有所帮助。


4
投票

如果你将UseSubmitBehavior="True"改为UseSubmitBehavior="False"你的问题将会得到解决

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

3
投票

我遇到了同样的问题,我做了什么:

刚刚添加条件if(!IsPostBack),它工作正常:)


3
投票

此错误将显示没有回发

添加代码:

If(!IsPostBack){

 //do something

}

2
投票

在这种情况下,将id添加到网格的RowDataBound中的按钮。它会解决你的问题。


2
投票

解决此问题的一个简单方法是对页面加载使用IsPostBack检查。这将解决这个问题。


177
投票

你的Page_Load活动中有代码吗?如果是,那么也许通过添加以下将有所帮助。

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

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


2
投票

Ajax UpdatePanel实现了它,我认为这是最简单的方法,忽略了Ajax回发开销。


2
投票

我知道这是一个超级老帖子。假设您正在调用您的应用程序,这是一个对我有用的想法:

  1. 在页面上实现ICallbackEventHandler
  2. 调用ClientScriptManager.GetCallbackEventReference来调用服务器端代码
  3. 如错误消息所示,您可以调用ClientScriptManager.RegisterForEventValidation

如果您不需要完全控制,可以使用更新面板来执行此操作。


2
投票

当我们将常规ASPX页面转换为内容页面时,我们遇到了同样的问题。

具有此问题的页面在其中一个内容部分中有一个</form>标记,因此在运行时呈现了两个表单结束标记,从而导致此问题。从页面中删除额外的表单结束标记解决了此问题。


1
投票

四分钟前我收到同样的错误。然后我像你一样在半小时内研究过。在所有论坛中,他们通常会说“添加页面enableEvent .. = false或true”。在找到之前,任何提出的解决方案都没有解决我的问题。不幸的是,这个问题是一个ASP.NET按钮。我在两秒前删除了它。我试图用“imagebutton”替换,但它也是不可接受的(因为它给出了同样的错误)。

最后我用LinkButton取代了。它似乎工作!


1
投票

我正在使用datalist,我的按钮出现了同样的错误。我只是使用IsPostBack检查并填充我的控件,问题解决了!大!!!


1
投票

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

1
投票

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


1
投票

如果您使用的是Ajax更新面板。添加<Triggers>标签并在其内部触发按钮或控件使用<asp:PostBackTrigger .../>导致postBack


1
投票

如果您事先知道可以填充的数据,则可以使用ClientScriptManager解决此问题。我在以前的用户选择中使用javascript动态填充下拉框时出现此问题。

下面是一些示例代码,用于覆盖render方法(在VB和C#中)并声明下拉列表ddCar的潜在值。

在VB中:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    Dim ClientScript As ClientScriptManager = Page.ClientScript

    ClientScript.RegisterForEventValidation("ddCar", "Mercedes")

    MyBase.Render(writer)
End Sub

或者C#的轻微变化可能是:

protected override void Render(HtmlTextWriter writer)
{
    Page.ClientScript.RegisterForEventValidation("ddCar", "Mercedes");
    base.Render(writer);
}

对于新手:这应该放在文件后面的代码(.vb或.cs)中,或者如果在aspx文件中使用,你可以包装在<script>标签中。


1
投票

这就是我得到它的原因:

我有一个ASP:ListBox。最初它被隐藏了。在客户端,我会通过AJAX填充选项。用户选择了一个选项。然后,当单击“提交”按钮时,服务器会对ListBox有所了解,因为它不记得它有任何选项。

所以我做的是确保在将表单提交回服务器之前清除所有列表选项。这样服务器没有抱怨,因为列表进入客户端空,它回来了空。

排序!


39
投票

我有过DataGrid的经验。其中一个栏目是“选择”按钮。当我点击任何行的“选择”按钮时,我收到此错误消息:

“无效的回发或回调参数。使用配置或页面中的<%@ Page EnableEventValidation =”true“%>启用事件验证。出于安全考虑,此功能验证回发或回调事件的参数是否来自服务器控件最初呈现它们。如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证。“

我改变了几个代码,最后我成功了。我的经历路线:

1)我将页面属性更改为EnableEventValidation="false"。但它没有用。 (出于安全原因,这不仅是危险的,我的事件处理程序也没有被调用:void Grid_SelectedIndexChanged(object sender, EventArgs e)

2)我在Render方法中实现了ClientScript.RegisterForEventValidation。但它没有用。

protected override void Render(HtmlTextWriter writer)
{
    foreach (DataGridItem item in this.Grid.Items)
    {
        Page.ClientScript.RegisterForEventValidation(item.UniqueID);
        foreach (TableCell cell in (item as TableRow).Cells)
        {
            Page.ClientScript.RegisterForEventValidation(cell.UniqueID);
            foreach (System.Web.UI.Control control in cell.Controls)
            {
                if (control is Button)
                    Page.ClientScript.RegisterForEventValidation(control.UniqueID);
            }
        }
    }
}

3)我将网格列中的按钮类型从PushButton更改为LinkButton。有效! (“ButtonType =”LinkBut​​ton“)。我认为如果你可以在其他情况下将按钮更改为其他控件,如”LinkBut​​ton“,它将正常工作。


1
投票

正如尼克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 +中出现


27
投票

你真的想做2或3,不要禁用事件验证。

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

  • 首先是它干扰了事件验证。返回服务器的不是它发送的内容。
  • 第二个是即使您禁用事件验证,当您的页面被回发后,列表框中的项目将从视图状态重建,因此您在客户端上所做的任何更改都将丢失。这样做的原因是asp.net不希望在客户端上修改列表框的内容,它只需要进行选择,因此它会丢弃您可能做出的任何更改。

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

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

总而言之,这是一个非常麻烦的解决方案,我不会真正推荐,但如果你真的必须对listBox进行客户端修改,它确实有效。不过,我真的建议你在走这条路之前先看一下updatePanel。


17
投票

我在Repeater中遇到了同样的问题,因为我在一个启用了EnableEventValidation的网站上有一个带有Repeater控件的网页。这不好。我收到了无效的回发相关异常。

对我有用的是为Repeater设置EnableViewState =“false”。优点是它使用起来更简单,就像为网站或网页切换事件验证一样简单,但范围远远小于切换事件验证。


16
投票

以上都不适合我。经过多次挖掘后,我意识到我忽略了在页面上应用的2个表单,这些表单导致了问题。

<body>
<form id="form1" runat="server">
<div>
        <form action="#" method="post" class="form" role="form">
        <div>
        ...
        <asp:Button ID="submitButton" runat="server"
        </div>
</div>
</body>

请注意,最近ASP.NET已开始考虑在iframe文档中包含表单标记的表单标记中的iframe作为嵌套框架。我不得不从表单标签中移出iframe以避免此错误。


12
投票

在客户端上使用JavaScript修改ListBox时遇到了同样的问题。当您从客户端向ListBox添加新项目时,会发生这种情况。

我找到的修复是通知事件验证系统可以从客户端添加的所有可能的有效项目。您可以通过覆盖Page.Render并为JavaScript可以添加到列表框的每个值调用Page.ClientScript.RegisterForEventValidation来执行此操作:

protected override void Render(HtmlTextWriter writer)
{
    foreach (string val in allPossibleListBoxValues)
    {
        Page.ClientScript.RegisterForEventValidation(myListBox.UniqueID, val);
    }
    base.Render(writer);
}

如果列表框中有大量可能有效的值,这可能会很麻烦。在我的情况下,我在两个ListBox之间移动项目 - 一个具有所有可能的值,另一个最初为空,但是当用户单击按钮时,用JavaScript中的第一个值的子集填充。在这种情况下,您只需要遍历第一个ListBox中的项目并使用第二个列表框注册每个项目:

protected override void Render(HtmlTextWriter writer)
{
    foreach (ListItem i in listBoxAll.Items)
    {
        Page.ClientScript.RegisterForEventValidation(listBoxSelected.UniqueID, i.Value);
    }
    base.Render(writer);
}

9
投票

这里没有提到的另一种方法是继承ListBox

IE浏览器。

public class ListBoxNoEventValidation : ListBox 
{
}

ClientEventValidation键关闭属性System.Web.UI.SupportsEventValidation,如果您将其子类化,除非您明确地将其重新添加,它将永远不会调用验证例程。这适用于任何控件,并且是我发现通过控件基础(即,不是页面级别)在控件上“禁用”它的唯一方法。


7
投票

3:我将网格列中的按钮类型从“PushButton”更改为“LinkBut​​ton”。有效! (“ButtonType =”LinkBut​​ton“)我认为如果你可以在其他情况下将按钮更改为其他控件,如”LinkBut​​ton“,它将正常工作。

我希望我可以投票给你,阿米尔(唉,我的代表太低了。)我只是遇到了这个问题而且改变这个就像我的gridview上的冠军一样。稍微说一下,我认为有效的代码是:ButtonType =“Link”

我怀疑这是因为当您点击“修改”时,您的修改会更改为“更新”和“取消”,然后在提交时更改回“编辑”。而这些转变控制令.net感到不安。

© www.soinside.com 2019 - 2024. All rights reserved.