Jquery – ASP.net viewstate changes validation and jQquery AJAX

ajaxasp.netjquerypostback

Ok, so the problem is as follows: I'm using jQuery's AJAX in order to make behind the scene calls within the page (on events such as voting an item) and changing the content in the appropriate element. The problem occurs when I mix AJAX with ASP.net's AJAX, more precisely when I try to do a postback AFTER I've used jQuery on the page to perform an action. The page's viewstate is changed and validation fails (which would seem somewhat normal as a matter of fact).

My question is: can I disable the validation somehow so that I can perform postbacks combined with the chaged page viewstate? So far searching on how to disable it yielded no results.

A more practical example is on a comments page where I allow voting the comments and posting new comments as well. So should a user vote a comment and THEN post his own, the page's contents is changed, and thus validation fails. Also, I've tried placing the comment form within an update panel as to prevent the entire page from posting, but it still fails.

Of course I could use an alternate route and have a different page for handling the event and just call that via jQuery's AJAX, but I was wondering if I could do this by combining ASP.net and jQuery.

Thanks in advance.

Best Answer

If you want to disable viewstate verification, you can set it at the page or config level by using Page.EnableViewStateMac = false.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.enableviewstatemac.aspx

It's not necessarily a good idea though because the validation functionality is there to protect from viewstate tampering, which you'll be turning off...

If you're running into issues with invalid viewstate because of jQuery ajax calls, one option is to consider using the Ajax controls, such as the UpdatePanel. You can wrap certain controls and mark the UpdatePanel as conditional to ensure a small round trip. This will not interfere with viewstate and allow you to continue to use viewstate validation and ajax at the same time.

There may be ways to use jQuery ajax calls and not interfere with viewstate validation. Others may be able to highlight this approach.