图像按钮单击给出无效的回发错误 [英] Image Button click giving invalid postback error

查看:81
本文介绍了图像按钮单击给出无效的回发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Datalist控件,我在其中放置了一个ImageButton。



但是当我点击ImageButton时,我得到以下错误。

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



我试过在Page Directive中的EnableEventValidation =true当我执行 EnableEventValidation =false DataList的ItemCommand事件不会触发



仍然是错误相同。有人可以在这方面帮助我。

解决方案

在你的web.config中改变这个



< pre lang =C ++>< pages validateRequest = false enableEventValidation = false />





更改后尝试


在客户端完成页面加载之前点击按钮?



让浏览器加载完全然后单击图像按钮。将部分渲染数据再次发送到服务器可能会导致问题。



谢谢,

Hemant


它是一个EventValidation问题。这也只发生在嵌套在gridview中的ImageButton。



 protected void Page_Load(object sender,EventArgs e)
{
// if(!IsPostBack)
// {
BindGrid();
//}
}

private void BindGrid()
{
ArrayList al = new ArrayList();
al.Add(a);
al.Add(b);
al.Add(c);
GridView1.DataSource = al;
GridView1.DataBind();
}

protected void ImageButton1_Click(object sender,EventArgs e)
{
}
< / script >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > Untitled Page < / title >
< span class =code-keyword>< / head >
< body >
< 表格 id = form1 runat = 服务器 >
< div >
< 如p:GridView ID = GridView1 runat = server

< span class =code-attribute> OnRowDataBound = GridView1_RowDataBound >
< >
< asp:TemplateField >
< ItemTemplate >
< asp:ImageButton ID = ImageButton1

OnCommand = ImageButton1_Click runat = < span class =code-keyword> server / >
< / ItemTemplate >
< / asp:TemplateField >
< / Columns >
< / asp:GridView >
< / div >
< ; / form >
< / body >
< / html >





正如您从以下示例代码中可以看到的,我们即使在回发时也会重新绑定网格,这意味着当用户通过我们的ImageButton回发时,网格将会反弹再次。发生这种情况时,gridview将重新创建gridview Vs从viewstate中提取值。通过在page_load方法中再次调用DataBind,Gridview重新创建子控件,当我们的页面进入Postback事件处理阶段时,在我们的imagecontrol中触发了RaisePostBackEvent(因为它实现了IPostBackEventHandler接口)。



RaisePostBackEvent使用两个参数调用ValidateEvent方法。第一个参数是ImageButton的UniqueID,第二个参数是字符串参数。这里的问题是UnqiueID参数。它具有错误控制的id。由于gridview是一个模板控件,它在控件前面加上一个唯一的命名空间,该命名空间恰好是当前行的rowID,它与gridview的id一起被显示。只有在RaisePostBackEvent期间,它的行ID才会出错。



您可以尝试在gridview中为ItemTemplate生成的行设置唯一ID。你可以在RowCreated方法或RowDataBound方法中尝试这个。通过这种方式,我们不再依赖于INamingContainer为行生成唯一ID,而后者又在我们的控件上得到了前缀,因为该行现在是直接NamingContainer并且问题已解决,例如:



  protected   void  GridView1_RowCreated( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType .DataRow)
e.Row.ID = e.Row.RowIndex.ToString();
}





互联网上很少有解决方案申请if(!IsPostBack)将解决问题,是的,但仅限于例如,如果您不想在编辑行时重新绑定网格。如果你想在ImageButton的帮助下删除一个记录后重新绑定网格,并在编辑一行后重新绑定它,那么你需要应用上述解决方案,这对两个方案都非常有效。


I have a Datalist control in which i have placed a ImageButton.

"But when i click the ImageButton I get following Error.
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."

I tried EnableEventValidation="true" in Page Directive and when I do EnableEventValidation="false" the ItemCommand event of DataList does not fires

Still the error is same. Can Some one Help me in this regard.

解决方案

in your web.config change this

<pages validateRequest="false" enableEventValidation="false" />



try after changing


Are you clicking the button before page load is finished at client end?

Let the browser load completly and then click on image button. sending partial rendered data to server again can cause the issue.

Thanks,
Hemant


Its an EventValidation issue. This also occurs only with an ImageButton nested in a gridview.

protected void Page_Load(object sender, EventArgs e)
{
  //if (!IsPostBack)
  //{
   BindGrid();
  //}
 }

 private void BindGrid()
 {
  ArrayList al = new ArrayList();
  al.Add("a");
  al.Add("b");
  al.Add("c");
  GridView1.DataSource = al;
  GridView1.DataBind();
}

protected void ImageButton1_Click(object sender, EventArgs e)
{
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView  ID="GridView1" runat="server"

           OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1"

          OnCommand="ImageButton1_Click" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>



As you can see from the following sample code, we are rebinding our grid even on a postback, this means when the user posts back via our ImageButton, the grid will get rebound again. When this happens the gridview will recreate the gridview Vs pulling values from viewstate. By calling DataBind again in the page_load method, the Gridview recreates the child controls, and by the time our page gets to the Postback event handling phase, RaisePostBackEvent is fired in our imagecontrol(since it implements the IPostBackEventHandler interface).

RaisePostBackEvent is what calls the ValidateEvent method, with two arguments. 1st argument is the UniqueID of our ImageButton and the second argument is a string argument. The issue here is with the UnqiueID argument. It has the id of the wrong control. Since the gridview is a templated control, it prefixes the control with a unique namespace which happens to be the rowID of the current row which gets appeneded along with the gridview's id. Its this row id going wrong somehow only during RaisePostBackEvent.

You can try setting a unique id for the row generated by the ItemTemplate in our gridview. YOu can try this in either the RowCreated method or the RowDataBound method. In this manner we do not depend anymore on INamingContainer generating a uniqueID for the row, which in turn gets prefixed on our control since the row is now the direct NamingContainer and the problem is solved, eg :

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
        e.Row.ID = e.Row.RowIndex.ToString();
}



Few solutions over the internet states applying if(!IsPostBack) will resolve the issue, true, but only in case if you do not want to re-bind your grid on editing a row as well. If you want to rebind grid after deleting a record with the help of ImageButton and rebind it after editing a row as well then you need to apply the above solution which perfectly works for both the scenarios.


这篇关于图像按钮单击给出无效的回发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆