1 min read

WebForms IsPostBack - Lies!

My first experience with ASP.NET was in 2006 - WebForms 2.0 was new. Obviously it's no longer new, revolutionary or anything else. My experience was entirely on desktop and command-line applications when I picked up my first book on developing applications.

I had no idea what the difference between GET and POST were. Hence, I took the IsPostBack property of the Page class at face value. The usage therefore pretty much went as follows:

public class MyWebForm : WebForm
{
	Page_Load(object sender, EventArgs e)
	{
    	if (IsPostBack) 
		{
        	// Handle the processing for when the form is returned
        }
        else
		{
        	// Initial work/processing to present the page/form to the user
		}
    }
}

It turns out that IsPostBack does not actually imply "form submitted using a POST method" (logical assumption in my humble opinion). Granted, a little RTFM could have gone a long way, but I was still learning the ropes.

Noting the formal documentation (at least as today) [1] :

Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.

Seems very helpful.

If you actually want to confirm GET vs POST, use the following:

if (Request.HttpMethod == "POST")
{
    // Etc. 
}

  1. MSDN Documentation "Page.IsPostBack Property" ↩︎