Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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.
Posted
Updated 4-May-11 22:57pm
v2
Comments
Legor 5-May-11 3:13am    
I guess its worthy to know what happens or what you would like to happen if one clicks the imagebutton. Or isn't there any code for this click event yet and you still got this problem?
suhailnoorpawane 5-May-11 3:38am    
It should navigate to another page but before that i am trying to set a value in session
I am pasting the aspx code down and ItemCommand Event of DataList code of aspx.cs too Please check it and reply

aspx
ImageUrl='<%#GetImage(Convert.ToString(DataBinder.Eval(Container.DataItem,"imagename")))%>' Width="75" Height="75"
CommandArgument='<%#Bind("sku")%>' CommandName="Product_Detail"/>

aspx.cs
protected void dlProductList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Product_Detail")
{
Session["Product_Name"] = Convert.ToString(e.CommandArgument);
Response.Redirect("~/Store/DemoProduct.aspx");
}
}

in your web.config change this

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


try after changing
 
Share this answer
 
Comments
suhailnoorpawane 5-May-11 3:35am    
Where exactly should i add this in Web.Config
suhailnoorpawane 5-May-11 3:50am    
I did that changes but its still not working Its not firing the ItemCommand Event of DataList But the Error is gone
as the Page is Not going any ever its on the same page.

I am using the ItemCommand Event Of Datalist
For two item In the Datalist
i.e ImageButton and LinkButton on clicking on both it should go to other page but before that i am setting a value in the sesssion

When i click on LinkButton its work fine

when i click on imagebutton it was giving error.
but as per your change i did the error gone but the event is not firing
Ashishmau 5-May-11 4:16am    
Have u databind of your gridview in if(!IsPostback). If not, then do it and try
suhailnoorpawane 5-May-11 4:56am    
i didnt get u
Ashishmau 5-May-11 5:02am    
On Page Load, Have u written Gridview code under if(!IsPostback){}. If not written then write it and try
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
 
Share this answer
 
Comments
suhailnoorpawane 5-May-11 5:29am    
NO after loading the page only i am clicking
Its an EventValidation issue. This also occurs only with an ImageButton nested in a gridview.

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

C#
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.
 
Share this answer
 
check for Page postback in page load
page load event()
{
if(!Page.IsPostBack)
{
//your code
}
}
 
Share this answer
 
Do you have codes in you Page_Load events? if yes, then perhaps by adding the following will help.
C#
if (!Page.IsPostBack)
{ //do something }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900