Jul*_*s A 233 .net asp.net postback argumentexception .net-2.0
当我从客户端发回页面时,我收到以下错误.我有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
Run Code Online (Sandbox Code Playgroud)
小智 180
你的Page_Load活动中有代码吗?如果是,那么也许通过添加以下将有所帮助.
if (!Page.IsPostBack)
{ //do something }
Run Code Online (Sandbox Code Playgroud)
当您单击命令并再次运行Page_load时会抛出此错误,在正常的生命周期中将是Page_Load - > Click on Command - > Page_Load(再次) - > Process ItemCommand Event
nor*_*rtB 169
问题是ASP.NET无法了解这个额外的或删除的listitem.你有很多选择(如下所列):
我希望这有帮助.
小智 39
我有过DataGrid的经验.其中一个栏目是"选择"按钮.当我点击任何行的"选择"按钮时,我收到此错误消息:
"无效的回发或回调参数.使用配置或页面中的<%@ Page EnableEventValidation ="true"%>启用事件验证.出于安全考虑,此功能验证回发或回调事件的参数是否来自服务器控件最初呈现它们.如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证."
我改变了几个代码,最后我成功了.我的经历路线:
1)我将页面属性更改为EnableEventValidation="false".但它没有用. (出于安全原因,这不仅是危险的,我的事件处理程序没有被调用:void Grid_SelectedIndexChanged(object sender, EventArgs e)
2)我ClientScript.RegisterForEventValidation在Render方法中实现.但它没有用.
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
3)我将网格列中的按钮类型从PushButton更改为LinkButton.有效!("ButtonType ="LinkButton").我认为如果你可以在其他情况下将按钮更改为其他控件,如"LinkButton",它将正常工作.
小智 27
你真的想做2或3,不要禁用事件验证.
向asp:listbox客户端添加项目有两个主要问题.
首先是它干扰了事件验证.返回服务器的不是它发送的内容.
第二个是即使您禁用事件验证,当您的页面被回发后,列表框中的项目将从视图状态重建,因此您在客户端上所做的任何更改都将丢失.这样做的原因是asp.net不希望在客户端上修改列表框的内容,它只需要进行选择,因此它会丢弃您可能做出的任何更改.
最好的选择是最有可能使用建议的更新面板.另一个选择,如果你真的需要做这个客户端,是使用普通的旧<select>而不是<asp:ListBox>,并将你的项目列表保存在隐藏的字段中.当页面在客户端上呈现时,您可以从文本字段内容的分割中填充它.
然后,当您准备发布它时,您将从修改后的字段中重新填充隐藏字段的内容<select>.然后,当然,你必须在服务器上再次拆分它并对你的项目做一些事情,因为你的选择是空的,因为它已经回到了服务器上.
总而言之,这是一个非常麻烦的解决方案,我不会真正推荐,但如果你真的必须对listBox进行客户端修改,它确实有效.不过,我真的建议你在走这条路之前先看一下updatePanel.
Uma*_*aja 17
我在Repeater中遇到了同样的问题,因为我在一个启用了EnableEventValidation的网站上有一个带有Repeater控件的网页.这不好.我收到了无效的回发相关异常.
对我有用的是为Repeater设置EnableViewState ="false".优点是它使用起来更简单,就像为网站或网页切换事件验证一样简单,但范围远远小于切换事件验证.
big*_*les 17
以上都不适合我.经过多次挖掘后,我意识到我忽略了在页面上应用的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>
Run Code Online (Sandbox Code Playgroud)
请注意,最近ASP.NET已开始考虑在iframe文档中包含表单标记的表单标记中的iframe作为嵌套框架.我不得不从表单标签中移出iframe以避免此错误.
Jos*_*osh 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);
}
Run Code Online (Sandbox Code Playgroud)
如果列表框中有大量可能有效的值,这可能会很麻烦.在我的情况下,我在两个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);
}
Run Code Online (Sandbox Code Playgroud)
小智 9
这里没有提到的另一种方法是继承ListBox
IE浏览器.
public class ListBoxNoEventValidation : ListBox
{
}
Run Code Online (Sandbox Code Playgroud)
ClientEventValidation键关闭属性System.Web.UI.SupportsEventValidation,如果您将其子类化,除非您明确地将其重新添加,它将永远不会调用验证例程.这适用于任何控件,并且是我发现通过控件基础(即,不是页面级别)在控件上"禁用"它的唯一方法.
小智 8
如果您通过客户端脚本填充DropdownList,则在将表单提交回服务器之前清除列表; 然后ASP.NET不会抱怨,安全性仍然存在.
要获取从DDL中选择的数据,可以将"OnChange"事件附加到DDL,以使用Style ="display:none;"在隐藏的Input或文本框中收集值.
小智 7
3:我将网格列中的按钮类型从"PushButton"更改为"LinkButton".有效!("ButtonType ="LinkButton")我认为如果你可以在其他情况下将按钮更改为其他控件,如"LinkButton",它将正常工作.
我希望我可以投票给你,阿米尔(唉,我的代表太低了.)我只是遇到了这个问题而且改变这个就像我的gridview上的冠军一样.稍微说一下,我认为有效的代码是:ButtonType ="Link"
我怀疑这是因为当您点击"修改"时,您的修改会更改为"更新"和"取消",然后在提交时更改回"编辑".而这些转变控制令.net感到不安.
(1)EnableEventValidation ="false"...................它对我不起作用.
(2)ClientScript.RegisterForEventValidation ....它对我不起作用.
解决方案1:
在GridView中将Button/ImageButton更改为LinkButton.有用.(但我喜欢ImageButton)
研究:Button/ImageButton和LinkButton使用不同的方法进行回发
来源文章:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
来源文章:
http://www.c-sharpcorner.com/Forums/Thread/35301/
小智 6
我实现了一个嵌套的网格视图,我遇到了同样的问题.我使用了LinkButton而不是像这样的图像按钮:
在我有这样一个列之前:
<asp:TemplateField ItemStyle-Width="9">
<ItemTemplate>
<asp:ImageButton ID="ImgBtn" ImageUrl="Include/images/gridplus.gif" CommandName="Expand"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
我已经这样替换了.
<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>
Run Code Online (Sandbox Code Playgroud)
小智 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);
Run Code Online (Sandbox Code Playgroud)
我的"HF"(hiddenfield)的选项由换行符分隔,如下所示:
HF.value = "option 1\n\roption 2\n\roption 3";
Run Code Online (Sandbox Code Playgroud)
问题是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);
Run Code Online (Sandbox Code Playgroud)
希望这能有所帮助.