Wednesday, July 7, 2010

RegisterStartupScript vs Response.Write

Even though putting windows popup alerts into your UI can be bad design, annoying to your users, etc, sometimes it's just what you need to get the job done. I feel it's ok to use an occasional windows alert, as long as you don't abuse them.

If you are also using Ajax, you have a choice of 2 ways to program that alert from the code behind: Response.Write or ScriptManager.RegisterStartupScript.

The difference is that Response.Write will popup an alert box before the page loads, ie on a white background, and ScriptManager.RegisterStartupScript will pop it up after the page has loaded, so the page is visible behind the popup.

I prefer using ScriptManager.RegisterStartupScript, but maybe I just haven't come across an application where Response.Write would be better.

In any case, the following is the code to use for each case:

Response.Write("<script language='javascript'> 
      alert('This pops up on a blank page
     (ie before the page loads) using response.write');</SCRIPT> "
);
ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, 
  "alert('This pops up after the page loads,
     using ScriptManager.RegisterStartupScript');"
, true);
 


I have also popped up an alert on pageload by using the following in the html < body> tag of the page:

<body onLoad="alert('Update. \r\n \r\n Thank you for your interest,
\r\n however, \r\n we are not taking any new orders at this time.')"
>
The \r\n causes a line break.

No comments:

Post a Comment