Javascript Alert Message from code behind in ASP.NET

How to show javascript alert message from code behind? You probably know, 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 javascript alert message both in AJAX environment and on 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);

    }

 }


Wednesday, March 19, 2008 | Comments (11) | Add Comment

Comments

Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

can't get the page value from the following line Page page = HttpContext.Current.Handler as Page; it always shows null..... wat is d problem....help needed

7/25/2008 10:00:47 PM | by Malik
Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

Really nice this is very helpful and easy.

6/17/2009 7:12:10 AM | by Arshika
Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

This works great, but if i use a response.redirect it doesnt come up, the page just transfers from one page to another without showing the aler, can somebody help please?

8/11/2009 1:53:06 PM | by reTkom
Gravatar

Don't do Response.Redirect. Modify code as follow, so it can do alert and redirect:

ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');window.location='newpage.aspx';", true);

Otherwise you need to pass your message in query string and then on the destination page do ScriptManager.RegisterStartupScript in Page_Load event.

Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

Hi,
Same problem i have..i having alert script in my code behind. after the alert script i put response.redirect...
It will transferred to new page without showing alert message..
How do i resolve them..

2/8/2010 9:44:37 PM | by Prabakaran
Gravatar

ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');window.location='newpage.aspx';", true);

Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');window.location='newpage.aspx';", true);

The window.location not work..but the alert message is shown

2/9/2010 12:07:33 AM | by Prabakaran
Gravatar

Does it give javascript error? Try to specify absolute path.

Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

Viktar-

After many, many hours of searching for a solution to showing an alert from my C# code behind while implementing AJAX on my HTML page, I finally came across your solution. It works great! Microsoft owes you big time for this easy solution.

Luo Long

2/9/2010 10:07:36 AM | by Luo Long
Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

thanks it works for me

2/10/2010 10:57:30 PM | by hemant
Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

thank you very very mach, I tried to find solutioon for this ussue for a long time, and this one so simple and elegant!!

4/11/2010 11:20:19 PM | by Irina
Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

Really nice this is very helpful and easy.

4/26/2010 12:23:24 AM | by Sudheer
Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

Thanks, this did the trick for me. Other approaches I tried either painted the alert box against a blank background or against a web page that hadn't finished drawing or else actually messed up the web page. This one worked perfectly.

6/29/2010 1:06:06 PM | by Jerry Elbow
Gravatar

Re:Javascript Alert Message from code behind in ASP.NET

Grt ........its working.......
thnak u..........

7/28/2010 12:00:16 AM | by Shakti Raturi

New Comment

Your Name:
Email (for internal use only):
Subject:
Comment:
 
Code above:

Categories

Recent Tweets

Valid XHTML 1.0 Transitional