Tutorial :How do I determine if a request is the result of a postback?



I'm implementing a custom page caching solution and I don't want the request to be cached or retrieved from the cache if it's in response to a form submission or some sort of asp.net postback.

I'm trying to figure out if the current HttpRequest is a postback. Is there a way of doing this outside the context of a page or other usercontrol? In otherwords if I'm inside an HttpModule I don't have access to this.IsPostBack but I still need to determine if it is in fact a postback.

Also, are postbacks always "Post" requests or is that determined by containing form?



Check the Method property of the HttpWebRequest. Postbacks should be marked as POST in the Method.

Also, the way you did it in old-school asp was to check for expected post-back parameters in the body of the HTTP message (Request.Form). You could check the content of the request for data that looks like postback parameter. I'm not sure what object exactly you're working with, but if it's an HttpWebRequest, you might check the request stream from the GetResponseStream() method of the object.


if (Request.ServerVariables["REQUEST_METHOD"] == "POST") {      // This is a POST  }  


The following static routine should be able to determine whether or not the current request is a postback. However, it will only work if you are executing an ASPX page, or derivitive thereof.

public class PostBackUtility  {      public static Boolean IsPagePostBack      {          get           {              Page pageHandler = HttpContext.Current.CurrentHandler as Page;              if (pageHandler == null) return false;              return pageHandler.IsPostBack;          }      }  }  

Should also note that CurrentHandler may not yet be initialised depending upon which stage in the request lifecycle you attempt to evaluate IsPagePostBack. I believe this method will only be valid between HttpApplication.PostMapRequestHandler and HttpApplication.ReleaseRequestState.


An HTTPHandler will have access to the current HTTPContext. You use that to check a few properties (Request.RequestType, Request.URLReferrer) and manually decide if it's a PostBack.


You can get a reference to the current page: Get current System.Web.UI.Page from HttpContext?

Then you can check for the Page.IsPostBack property.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »