The easiest way to break ASP.NET AJAX pages
AJAX, ASP.NET By Dave Ward on June 19th, 2007Use 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:
<asp:ScriptManager ID="sm1" runat="server" /> <asp:UpdatePanel ID="up1" runat="server"> <ContentTemplate> <asp:Literal ID="Literal1" runat="server" Text="Update Me!" /> <br /> <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" /> </ContentTemplate> </asp:UpdatePanel>
Literal1.Text = DateTime.Now.Millisecond.ToString(); Response.Write("<script>alert('" + DateTime.Now.Millisecond + "');</script>");
However, this code will result in a cryptic, JavaScript alert when used in a partial postback:

The proper way to do this in ASP.NET AJAX is to use the ScriptManager:
Literal1.Text = DateTime.Now.Millisecond.ToString(); ScriptManager.RegisterStartupScript(Page, GetType(), "MyScript", "alert(" + DateTime.Now.Millisecond + ");", true);

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!
Similar posts
What do you think? Your comments are welcomed.
I appreciate all of your comments, questions, and other feedback, but please try to stay on topic. If you have a question unrelated to this post, I recommend posting on the ASP.NET forums. If you post there and then contact me with a link to the post, I'll try to take a look at it for you.
If you're replying to an existing comment, please use the threading feature. To do this, click the "Reply to this comment" link underneath the comment you're replying to.

Comments
You seriously come across Response.Write in ASP.NET projects these days?
Sadly, yes.
Great post, I’ve been confused by that for a long time.
great site, this article, jus “saved my life” thanks. can u tell me how u got the images as icons in these textboxes? looks cool.
It’s accomplished via CSS. The images are backgrounds to the input elements, and the elements have padding-left set to offset the first character to the right of the image.
had a feelin it was the lovely css, was lookin in the css section of ur site 4 examples/tutorials on how to do dat?, i dont suppose u can point me in the right direction 4 examples? wanna add it to some asp.net pages. cool site design by da way.
This is the CSS for the “Name” field of this form, to get you started:
thanks, i’m a try it out
This is Very Use full for me.Thanks,,,…