I have ASP.NET site. At one point postback is triggered and some data is modified on server and response is sent (with new view state).
Problem is that I use jQuery to show only a portion of that response on page. That works, but I’m having problems updating view state with new value.
I have somethig like this:
var updatePreviewArea = function (nid) {
var $content = jQuery('<div></div>');
$content.load('http://site.com?nid=' + nid, function (response) {
var $response = jQuery(response);
jQuery('targetDiv1').replaceWith($response.find('#srcDiv1'));
jQuery('targetDiv2').replaceWith($response.find('#srcDiv2'));
// update viewstate from postback response
var selectors = ['#__VIEWSTATE', '#__EVENTVALIDATION'];
for (var i in selectors) {
var value = $response.find(selectors[i]).val();
jQuery(selectors[i]).val(value);
}
});
}
But after this I get:
Sys.WebForms.PageRequestManagerServerErrorException:
Sys.WebForms.PageRequestManagerServerErrorException: Validation of
viewstate MAC failed. If this application is hosted by a Web Farm or
cluster, ensure that configuration specifies the same
validationKey and validation algorithm. AutoGenerate cannot be used in
a cluster.’ when calling method: [nsIDOMEventListener::handleEvent]
You are taking the viewstate that was returned from the server and updating your content page with it. ViewState is basically an encoded set of name/value pairs that needs to match up with the content on the page that it’s related to. If you take the ViewState of one page and stick it on another, the encrypted value won’t match up and you’ll get an error that’s similar to what you’re seeing.
Since you’re using jQuery and AJAX to dynamically get content from your server, I’m not sure why you’re depending on ViewState (but I’m not familiar with your application).