Tuesday, November 07, 2006

Clearing up Page.IsPostBack() property (asp2)*

Tuesday, November 07, 2006 4:00:03 PM (GMT Standard Time, UTC+00:00)

In asp2 the Page.IsPostBack() property comes in rather handy at times but the key to its use is having a clear understanding of the property and when exactly it returns true or false.

To help me determine this, I checked the MSDN library and found the following Summary for the property: "Gets a value indicating whether the page is being loaded in response to a client postback (in which case it returns TRUE), or if it is being loaded and accessed for the first time (in which case it returns FALSE)."

Sad to say, this summary is actually a tad misleading. When it comments "or if it is being loaded and accessed for the first time", this to me gives the impression that ONLY the FIRST TIME that page is loaded/accessed (by that user), the property will return FALSE. This however, is not the case.

The actual times when the Page.IsPostBack() property returns FALSE are: a) when the page is being accessed (by that user) for the first time AND also b) if the page even though accessed already is being accessed again from another page (ie you went to Page 1 already but currently have browsed to Page 2... Now, whilst on Page 2 you click a link that takes you back to Page 1. Here, the Page.IsPostBack() property in Page 1 will return FALSE).

Hopefully this should clear things up a bit, it sure did for me.

Related posts:
Select a random row in MS SQL...
Regular expressions
VS2005, ASP.NET 2 & DLLs, DLLs..
MS's ASP.NET and.. PHP
2-way databinding cascading drop down lists within a FormView
ApplicationName Property when customising providers

Comments are closed.