Use Response.Write().

If you’re worried that might not bring your website to its knees reliably enough, be especially sure to use Response.Write in code that’s executed during partial postbacks. It’s that easy!

Emitting JavaScript probably comprises the vast majority of Response.Write uses in ASP.NET projects today. So for our purposes, let’s say that we really want to display a JavaScript alert window during a partial postback, from code behind. Using Response.Write, this is how the task would have been accomplished:

However, this code will result in a cryptic, JavaScript alert when used in a partial postback:

ASP.NET AJAX error due to Response.Write

The proper way to do this in ASP.NET AJAX is to use the ScriptManager:

ASP.NET AJAX response.write error fixed

It’s that easy.

Even if you’re currently getting away with Response.Write on your AJAX pages, there’s a good chance that your site will inexplicably break one day due to a change that doesn’t intuitively relate to the Response.Write. If that weren’t bad enough, these code behind AJAX errors can be abnormally difficult to resolve since the debugger in Visual Studio isn’t any help.

Leave Response.Write in the 90’s with those ASP 3.0 projects, where it belongs!