mlaen
mlaen

Reputation: 53

Updating viewstate from postback response

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]

Upvotes: 1

Views: 1651

Answers (3)

David Hoerster
David Hoerster

Reputation: 28701

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).

Upvotes: 3

Michael Liu
Michael Liu

Reputation: 55339

The encoded __VIEWSTATE is specific to the page that generated it. You will get a viewstate validation error if the page that your form posts to does not match the page that generated the viewstate. You have two options:

  1. Send your AJAX request to the same page, not a different page.
  2. Set the post URL of the form to the URL of the other page.

Upvotes: 1

Icarus
Icarus

Reputation: 63956

I wouldn't go this route (updating the ViewState manually) as this is not an easy task; at the very least, ViewState is a BASE64 encoded string and you'll have to write quite a good deal of javascript code to always keep things in sync. I see this becoming a nightmare to maintain. In your shoes, I would be using strictly ajax instead allowing post backs like this or let MS handle ViewState for you, and use UpdatePanels but I dislike those too.

Further, if your app for some reason, ever needs to encrypt ViewState, you'll not be having fun maintaining your app.

Upvotes: 2

Related Questions