ASP.NET AJAX Challenges
I’ve been playing with ASP.NET AJAX over the last several days, and I have to say that it’s very cool. It can also be hugely frustrating.
Here’s the scenario that wrapped me around the axle for a couple of hours tonight:
First, I created a test web project in Visual Studio 2005 and built a simple page with an UpdatePanel, a Timer control, and a Label. The Timer was set to fire once per second, and when it did, the Label was updated with the current time. Since it was in an UpdatePanel, the updates should all occur without page postbacks. Build, run…yep, it works. Cool.
Then I tried incorporating the same type of thing on an existing website. This one’s complex; there’s a Master Page, lots of controls that get pulled in…you name it, it’s in there. And right away, it starts doing weird stuff. The first time through it was causing a full page postback every time the Timer fired. That didn’t seem right. So I adjusted some things, and it would cause script errors every time the Timer fired. Heck, that’s worse.
The solution? Remove all instances of Response.Write in the code!
The error message suggests that Response.Write could cause problems, and what I found was that there were two places where I had used Response.Write for quick tweaks to page content (one was in the footer, where the Copyright date was being updated on the fly). A little manipulation to replace the Response.Writes to asp:Literals that get updated in the Page_Load event, and I was golden.
So that’s my neat-o solution for the day (posted because Google found precious little about this): If you’re adding ASP.NET AJAX to an existing website, and changes within an UpdatePanel are causing a full page postback or are causing script errors on the page, then you might want to look for places where you took a shortcut and used Response.Write within your .ASPX code…more than likely, that’s what’s getting in the way of the AJAX code.


