Strange issue with Application Page and Two Button Presses

Okay, this one was weird. The symptom is this – I had a custom Application page that was being used as the New and Edit forms for a particular custom list in my solution. One of the customisations was that it had an ‘Address Search’ button that, when you pressed, searched for an address based on the first line of the address, or the postcode.

That’s fine, but here’s where things got weird.

  • If you were using the form as a New form, then search worked just fine the first time you clicked it.
  • If you were using the form as an Edit form, the the search button didn’t work the first time you use it – but did the second time you clicked on it.

Bizarre, and I had to fix it…

I started looking at obvious things (i.e. my code) but that looked okay. I debugged the page lifecycle and found that my code wouldn’t break – heck, it didn’t even appear to be called – during the “first click in Edit more” scenario – but it did break on the second click. That’s pretty weird – clicking the same button should run the same code each time. I tried break pointing every function in my C# page-behind, and none of those breakpoints were hit. It was like the page wasn’t running at all, and yet I could see the form.

I began to think it was something fundamentally different about the page, so I decided to compare the source of my Edit form before and after that first click.

The first thing I noticed was that the files were different sizes. This seemed surprising, but I figured that things like the ViewState could account for that. So, next, I used WinMerge to highlight differences. There were 12 differences, mostly to do with transient data, such as the viewstate, and different digests. However, when I got to the middle of the file I found a difference in the controls being added to the page.

This is the Small Search box, which caught me off-guard. It appeared to have an extra control on the page before the first ‘search’ button click. Afterwards, it was gone.

By this point I was in a hurry, so I decided to try just removing the search box from the page. We don’t need it anyway. The search box is put in a ContentPlaceHolder control, so I decided to blank that out:

And mysteriously, my page and it’s search button started working again. I think that what’s happening is that the Search Box control loaded by the delegate is dynamically creating webcontrols. However, for some reason, on the postback it isn’t creating the same controls. This should cause an exception due to the invalid viewstate, but for some reason this wasn’t being shown. However, this exception would happen before any of my code was run – which is why none of my breakpoints would be hit on the first button press.

All in all, strange, but hopefully it might help someone else if they have the same problem.

Advertisements
Strange issue with Application Page and Two Button Presses

One thought on “Strange issue with Application Page and Two Button Presses

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s