Using AJAX to put up a MessageBox in the Browser in ASP.NET 2.0

I'm currently working on a consulting project in ASP.NET for a hospital group, and find myself putting more and more update panels on the pages to reduce the browser rendering for the user.

One of my pages requires the administrator to enter data about a worker, so I wanted to send feedback to the adminstrator in the form of a message box (i.e. a javascript alert) saying the insert completed okay.

When you are working inside an update panel, you need to exercise a method of the ScriptManager in order for your javascript to work.

I created this simple utility method to help me to utilize the ScriptManager to execute javascript from the server:

static public void DisplayAJAXMessage(Control page, string msg)
{
 
string myScript = String.Format("alert('"{0}"');",msg);
 
ScriptManager.RegisterStartupScript(page, page.GetType(),
    "MyScript"
, myScript, true);
}

You can simply pass in the current aspx page object in the code behind as the control  (i.e. this) 

So for example,  in your aspx.cs code behind, to put up a message box, simply call:

DisplayAJAXMessage(this, "Finished Entering Worker");

That's It!  A message box will show up just by calling this method from your code behind when working with update panels.  Hope you find this tip useful for your own web development.