views:

584

answers:

3

Hi,

When using jQuery to POST data back to the server, I'm getting some strange behavior.

If I include __VIEWSTATE and __EVENTVALIDATION in my serialized form data, the IsPostback page variable is set to true, If I exclude these two, the IsPostback is set to false.

Its easy enough for me to include these two variables but does anyone know whats going on behind the scenes?

+3  A: 

One of the things the viewstate and eventvalidation track are whether or not a postback has occurred. Considering the event-based ASP.NET page lifecycle, there is no other way to track if a postback has occured than by simply storing the value somewhere.

In other words, you could manually check for postback by digging into the viewstate object and finding the postback boolean and changing it to true. This may not be exactly as described, but it highlights what's going on inside the viewstate a little better.

Soviut
+1  A: 

IsPostBack() is a bit of a mess, but yes it's checking for __VIEWSTATE and __EVENTVALIDATION and not much else. What it certainly doesn't do is check if the HTTP verb is a POST or a GET, which most people assume (you can see this if you set up a simple page and put the viewstate and eventvalidation (if present) fields in the query string then check IsPostBack()

However why would you want to check? Really this only comes into play when a full form submission is sent, if you're partially posting you tend to do it to a web service where IsPostBack is unimportant.

blowdart
A: 

If you want to issue a "postback" with jquery, you should be including __VIEWSTATE and __EVENTVALIDATION. Those are two critical chunks of data that ASP.NET WebForms needs to be able to properly propogate a full postback. If you only wish to issue a service call, then you should not need either of those fields, but you will loose the ability to post to a page with all the benefits of the WebForms event pipeline.

jrista