Case of the missing Postback in IE

Here's one with a simple solution that took me way longer to figure out than it should have.

We recently ported our internal intranet over to ASP.NET 2 (I know it's 2008, but it was a big project). For the most part this went smoothly, but a few minor issues appeared. One of these issues was with a custom server control that managed data paging. The control would draw the appropriate number of link buttons, when a button was clicked it generated a postback and called a SelectedPageChanged event to do whatever was required.

Because this is a custom control, I had to manually wire up the postback event:

   1:  newLinkBtn.Attributes.Add("onclick", 
   2:      Page.ClientScript.GetPostBackEventReference(this, argument));

This worked great in ASP.NET 1.1, and even after the port worked fine in Firefox and Safari. IE however showed some very strange behavior. It was refreshing the page but nothing was happening, which is very strange considering the code was all server side. There shouldn't have been any differences in browser behavior.

My first step was to update the code to use the ASP.NET 2 ClientScript tools:

   1:  newLinkBtn.OnClientClick = 
   2:      Page.ClientScript.GetPostBackClientHyperlink(this, argument);

Which didn't help. Stepping through the debugger, I saw that the page lifecycle was happening twice. The first time everything happened as expected, however the second time, everything defaulted back to the first load. This left me scratching my head for a while (longer than I cared to admit). I wish I could say I came up with the solution myself, but some googling led me to this post on msdner.net.  Simply appending a return statement solved my problem:

   1:  newLinkBtn.OnClientClick = 
   2:      Page.ClientScript.GetPostBackClientHyperlink(this, argument);   
   3:  newLinkBtn.OnClientClick += ";return false;";

In a nutshell, if you manually wire up a GetPostBackClientHyperLink (or GetPostBackEventReference), it will no longer provide a safety return statement. I could get mad at this behavior, but I see some benefit in being able to chain your own javascript to the PostBack event... if it worked consistently in all browsers. As it is this behavior only cropped up in IE (both 6 and 7), which made it quite a pain to figure out.

Print | posted on Tuesday, February 12, 2008 4:17 PM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)
 
Please add 4 and 7 and type the answer here: