Friday, October 26, 2007

Invalid postback or callback argument solution. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page.

First of all I will give a brief overview of Postback and Callback. When ever you register a control's server side event, ASP.Net register a JavaScript which will submit the form's details and on the server it fires the event which is registered for the control.

ASP.net register __doPostBack javascript function. Syntax for the script is __doPostBack('Event Target','Event Argument'); Here Event Target can be a control or it can be a function created by developer. You can also generate a postback script reference for a control by using GetPostBackScript function. Now ASP.Net page always handle the PostBack event by it self.

For Callback you have to impliment a ICallBackEventHandler interface. After implimenting ICallBackEventHandler a page must contain two events, RaiseCallBackEvent with return type void and GetCallBackResult with return type string. Your code logic will be contained in the RaiseCallBackEvent function code block and the result of the Callback will be contained returned by GetCallBackResult. Function GetCallBackResult will call the client side callback result function. I will explain CallBack in detail in my future post.
Many of ASP.Net users are facing a problem with invalid Postback or Callback argument error. Invalid PostBack or CallBack argument error is basically raise because of Event Validation feature. The EventValidation feature is a new feature in ASP.NET 2.0, and provides an additional level of checks to verify that a postback from a control on the client is really from that control and not from someone malicious using something like a cross-site script injection to try and manipulate things. It is part of our overall strategy of increasingly adding security in depth levels to the programming model -- so that developers can be secure by default even if they forget to add security checks of their own.

Now, Invalid PostBack or CallBack argument error may occur when you are firing click event and the object is rebinding or its properties are changed in Page_Load event or someone is trying to hack into your system with cross site scripting. Each time .Net Framework render a page then it associate a unique Guid for all the controls. When binding a gridview or repeater, on each databind framework will associate a new guid for the contorl. So every time when you are firing event make sure Page_Load event does not change the control, because if the control changed the it will have a different Guid which have acutally fired the event for postback. Here are some scenario with this error.

1) Invalid Postback or Callback argument in GridView Problem may be: You are binding data in Page_Load event with either Object Data Source or Manual Binding with function call. This will make your GridView bind data on every event fire of any control. When you are firing any GridView command with OnRowCommand, before RowCommand fire your GridView will rebind and all control within it will be assigned to new id. So RowCommand could not get the item which have fired the event. Solution for Invalid Postback or Callback argument in GridView: You can bind your data within this if condition
1:  if (!IsPostBack)
2:  {
3:       //Your code for Bind data 
4:  }
This code will definitely give you solution if this not work then check whether any other control is not giving error.

2) Invalid Postback or Callback argument while submitting form. Problem may be: You have many textboxes and textareas and when user is entering "<" or ">" char then it is giving error. This is because of .Net framework is giving facility to validate request. This function will allow user not to submit script or html code directly so it is blocked. Solution for Invalid Postback or Callback argument while submitting form: You can replace this char with javascript before submitting the form replace "<" with "&lt;" and ">" with "&gt;" the javascript code is here
1:  function ReplaceChar(obj) //Here obj is your textbox object 
2:  {
3:      var Textvalue = obj.value;
4:      Textvalue = Textvalue.replace("<","&lt;");
5:      Textvalue = Textvalue.replace(">","&gt;");
6:      obj.value = Textvalue;
7:   } 


The other solution for both of this issue is to set enableEventValidation=false You can set this option declaration <@ Page > or even you can put this code in your web. config file in <system.web> block <pages enableeventvalidation="false">//set it true if you want to validate your each request. If you do not validate the request then your security of data will be decrease so this is not the perfect solution If both of this solution not work then you can contact me on my email and if you find any new solution please post it comment.