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.

31 comments:

  1. Thanks for providing such a wonderful information

    ReplyDelete
  2. I had this problem but it originated from another <form> tag that I had left in the page (apart from the mandatory <form runat="server"> tag), while converting from legacy ASP code.

    Removing this <form> tag (anyway I use Postback now) eliminated this error message.

    ReplyDelete
  3. I had the same error message but it originated from a <form> tag that I had left in the page while converting from legacy ASP code.

    Removing this tag and leaving the mandatory <form runat="server"> eliminated the error.

    ReplyDelete
  4. Great!!! Thanks a lot! Moving the code solved the problem.

    ReplyDelete
  5. I have the same error,it is solved using these solutions.
    Thanks...........
    Ramesh Air

    ReplyDelete
  6. I have the same error,it is solved using these solutions.
    Thanks...........
    Ramesh Ahir

    ReplyDelete
  7. I have the same error,it is solved using these solutions.
    Thanks...........
    Ramesh Ahir

    ReplyDelete
  8. E bhaila o tamaro khub khub aabhar......

    ReplyDelete
  9. Thanks a million for your insight into this issue.

    ReplyDelete
  10. Thanks for Providing the Vital Information that can easily sort out the issue.

    ReplyDelete
  11. i am sending Session Variable for back and forth the data for displaying gridview with same data, then i have this problem which is not remove by pages validateRequest="false" enableEventValidation="false"

    Plz help me.

    kapil gupta

    ReplyDelete
  12. Re: Invalid Postback or Callback Argument

    Hi Kapil,

    If you can post stack trace of your Invalid Postback or Callback argument then I can help you to solve Invalid Postback or callback argument

    Also check what is the eventargument in the postback or callback.

    ReplyDelete
  13. Thank you for this post! I had some problems with this specific error and didn't know where to look to solve this issue. After reading this post I solved the problem!

    ReplyDelete
  14. Thank for the vital information on Invalid Postback or Callback argument in asp.net. It was very helpful.

    ReplyDelete
  15. Just add this to your web config and make pages..like ErrorPage.aspx , and see you will control this error by making your custom control error.
    system.web

    customErrors defaultRedirect="ErrorPage.aspx" mode="On"
    error statusCode="500" redirect="servererror.aspx" error statusCode="404" redirect="filenotfound.aspx" error statusCode="403" redirect="AccessDenied.aspx" customErrors
    system.web include html tags Please

    ReplyDelete
  16. Thank you very much boss.
    I used
    If(!IsPostBack)
    {
    }
    in page_load sections and put my req. code there. now its working. thanks a lots.

    ReplyDelete
  17. Good day,

    Thank you for post. But look like that error can appear in other way too. I have 2 projects. In both of them time to time appear this error. But projects absolute different in databinding work part.
    1. In first peoject I was disable ViewState for Repeater and DataBind it on each page request from Session.
    2. Repeater boubnd one time on !Page.IsPostBack

    As I say, both of them time to time got such error in log.

    Is it present way to find what controll or code part raise this error.

    P.S. 2) Invalid Postback or Callback argument while submitting form - it's not true, in this way system raise HttpRequestValidationException: Exception message: A potentially dangerous Request.Form value ...

    ReplyDelete
  18. Hello, I am encountering this invalid postback issue that I've not been able to resolve fully.

    I have created a user control, that is essentially consists of an asp:linkbutton that acts as a banner of sorts, a modal popup, and some databound asp:dropdownslists (as well as an "OK" linkbutton and a "cancel" linkbutton).

    When a user clicks on the "banner" linkbutton, I use JavaScript to show the modal popup and set the selected values in the dropdown lists based upon the state when the user control was loaded on a page.

    The first dropdown selected value is used to determine what options are available for selection in the remaning two dropdowns. So, if the user changes the selection of the first dropdown, I have some JS to manipulate the available options in the other dropdowns.

    Once the user has chosen the desired selections from the dropdowns and clicks the "OK" linkbutton, I execute some JS to process the selections, hide the modal popup, and then call __doPostBack() with the ClientID of my "OK" linkbutton as the EVENTTARGET, and a given string as the EVENTARGUMENT that I then capture in the Page_Load method of the user control to specify what kind of processing to do on the user's selections in the code behind (C#).

    It was then that I started to get a .NET error:

    Invalid postback or callback argument. Event validation is enabled using in configuration or 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.

    at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) at System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    After some searching, I found a *potential* solution based upon overriding the user control's Render() method. In the Render() method, I have some C# code to do this:

    Page.ClientScript.RegisterForEventValidation(< dropdownlist >.UniqueID, < string matching the EVENTARGUMENT specified in the __doPostBack call >);

    I have one statement like this for each of the my dropdowns, and I also even have a function call to loop through each of the ListItems in the dropdowns and call RegisterForEventValidation(), using the dropdown's UniqueID and the given ListItem's value as parameters.

    Now, I have some success doing this, if my user control is simply on a Page or is contained in another basic user control. However, if my user control is being rendered in rows of gridview, I still get the Invalid postback or callback argument...

    Any help would be greatly appreciated.

    ReplyDelete
  19. Danny..

    It seems that you ara changing the values of Dropdowns with JS, correct?

    If so then you can check with EnableViewState="false" for that(those) dropdowns.

    Basically when you reload the page it binds all the drop downs with values from viewstate.

    Please check it. and let us know

    ReplyDelete
  20. Yes you are correct, I am using JS to update the options available in two dropdowns based on the first dropdown's selection... So, cascading dropdowns without having to do a postback to get the new options.

    Indeed I do have EnableViewState set to false for the three dropdowns.

    It is baffling me because my user control works properly without the invalid postback error until I attempt to use the control within a gridview row...

    ReplyDelete
  21. thank you verry much, i tried to override the render-method and use RegisterForEventValidation but only your solution was helpful

    ReplyDelete
  22. Your #1 solution worked for me, this issue had been plauging me for months!

    ReplyDelete
  23. Please help

    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

    at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
    at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument)
    at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    ReplyDelete
  24. Hello,
    thanks for this great informations. In my case the problem was due to the fact that I forgot to fill the DataTextField and the DataValueField in a DropDownList.

    Regards
    Vincenzo

    ReplyDelete
  25. Thanks for sharing the information.
    I am having the same problem.
    I have a dropdownlist with a ControlAdapter attached to it (for optgroup). AutoPostback is set to true. And on postback it is giving error.

    I am not able to solve it.

    Although by setting EnableEventValidation="false" for the Page directive, it is working. But there could be some other way.

    Please help..

    ReplyDelete
  26. Thank you very much

    ReplyDelete
  27. thanks a lot..this problem was driving me crazy!

    ReplyDelete
  28. Another useless post that doesn't solve anything

    ReplyDelete
  29. Your #1 solution worked for me,thanks

    ReplyDelete
  30. problem still exist

    ReplyDelete

Comments posted on ASP.Net Ajax Tutorials Blog are moderated and will be approved only if they are on-topic and not abusive. Please email me or my team for tech-support or blogging related questions. Avoid including website URLs in your comments - Thanks Author