I don’t know if any of you have experienced this, but it has been causing me grief for some time, but I finally solved it.
The Background
I have a web page that is part of the admin area of a client’s website. The page has a GridView with a list of the website’s members. The GridView is contained inside of an UpdatePanel control.
The Problem
When you click the “Edit” button for a row, and your browser is some version of Internet Explorer (this does not happen with Firefox), your Page_Load() method is called twice. The first time IsPostBack == false, and none of your ViewState variables are available, so as far as your code is concerned, it looks like a first time page load and any “!IsPostBack” logic you have is executed. The second call to Page_Load() is the normal one where .IsPostBack is true and your event handlers are called.
Since the ViewState variables are not present, you cannot set any sort of flag indicating the page has already been loaded. Setting Trace=”true” on the @Page directive in the .aspx file yields nothing because the page is loaded twice so the first one is overwritten by the second. I even tried setting an onunload handler in the client script to raise a server event (via Page.ClientScript.GetCallbackEventReference), but for some reason the onunload handler was never called, regardless of whether I set it on the window, document or body element.
The Solution
After trying all sorts of ways to reliably identify when a Page_Load() call was bogus, I finally came across this thread in the ASP.NET forums. Basically, the solution is very simple and easy – you check the Page.Request.RequestType property. If the RequestType is “GET”, it is the real first time page load. If it is “POST”, then it is a postback. So, I created the following property:
protected bool IsFalseFirstPageLoad
{
get { return (!this.IsPostBack &&
0 == String.Compare(this.Request.RequestType, "POST", true)); }
}
Then, at the top of my Page_Load() method, I have the following code:
if (this.IsFalseFirstPageLoad)
{
return;
}
As simple as that. Problem solved.

{ 1 trackback }
{ 0 comments… add one now }