Javascript Alert Message from code behind in ASP.NET
How to show a javascript alert message from a code-behind? You probably know, that we can do it like this:
ClientScript.RegisterStartupScript(Page, "message", "alert('Hellow world!');", true).
However, this method is not going to work in AJAX environment. For AJAX you need to use: ScriptManager.RegisterStartupScript.Here is a little utility method, which displays a javascript alert message both in AJAX environment and on a regular page:
public static void ShowAlertMessage(string error)
{
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
error = error.Replace("'", "'");
ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);
}
}