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);

    }

 }


Posted on Wednesday, March 19, 2008 by | Comments (37) | 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

Posted on 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.

Posted on 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?

Posted on 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..

Posted on 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

Posted on 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

Posted on 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

Posted on 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!!

Posted on 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.

Posted on 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.

Posted on 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..........

Posted on 7/28/2010 12:00:16 AM by Shakti Raturi #
Gravatar

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

this is the absolute business!!! thank you soo much for this post!!!

Posted on 9/8/2010 6:09:39 AM by ross #
Gravatar

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

it's realy nice ,go ahead buddy.

Posted on 9/10/2010 6:42:14 AM by ilavarasan Elango #
Gravatar

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

Hi,

Thanks for this great piece of code. Here it is for VB.Net (had to change it somewhat as some variable names are not allowed in VB.Net). I put it in a class that all my pages inherit (see below if you're new to that).
/Petter

The code, in a class called Mainclass, in the App_Code folder:
Public Shared Sub ShowAlertMessage(ByVal msg As String)
'Display javascript-alert
Dim page As Page = TryCast(HttpContext.Current.Handler, Page)

If page IsNot Nothing Then
msg = msg.Replace("'", "\")
ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" & msg & "');", True)
End If
End Sub

Top of the .aspx.vb page:
Imports Mainclass

Calling the code:

ShowAlertMessage("You are now registered.")

Posted on 11/10/2010 9:10:49 AM by Petter #
Gravatar

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

great work,keep it up...........

Posted on 11/16/2010 1:02:15 AM by Srikanth #
Gravatar

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

Good one, It worked.

Posted on 12/7/2010 3:05:09 AM by KB #
Gravatar

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

how display the message box in asp.net as click 'ok' to save and click 'no' then message wil unsave

Posted on 12/8/2010 7:13:20 AM by kiran bade #
Gravatar

Use javascript function confirm.
http://www.tizag.com/javascriptT/javascriptconfirm.php

Gravatar

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

Hey Thanx a lot:) it wz qyt helpful :)

Posted on 2/18/2011 1:44:01 AM by Maya #
Gravatar

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

THANK YOU!!!!!!!!
THANK YOU!!!!!!!!
THANK YOU!!!!!!!!

Posted on 3/30/2011 10:15:38 AM by asasdasd #
Gravatar

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

Thanks....
It's Works..

Posted on 4/27/2011 5:01:09 AM by Shikhar #
Gravatar

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

Thnxxxxxxxxxxxx a lot....

It solved my problem.

Previously scriptmanager was not working when type is taken as page.

"Page page = HttpContext.Current.Handler as Page;" this line solved my problem.....

Posted on 5/21/2011 3:42:46 AM by sandhya #
Gravatar

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

thanks a lot

Posted on 5/24/2011 8:25:15 AM by CPULI #
Gravatar

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

Thanks alot...really its very helping

Posted on 7/12/2011 10:05:32 PM by Shantanu #
Gravatar

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

thanks sir, i get it in my first search,thanks alot

Posted on 7/14/2011 12:35:19 AM by Pardeep kumar #
Gravatar

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

Hello Sir

Thanks for such a nice piece of code. It really help me in my program.

Thanks alot

Subhash

Posted on 7/19/2011 3:55:21 AM by Subhash Pande #
Gravatar

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

Well done!

Posted on 8/26/2011 8:55:41 AM by mark grizzle #
Gravatar

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

Thank you much, this script worked for me.

Posted on 10/5/2011 1:11:29 AM by neeta #
Gravatar

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

This code is working. But if we use this code on the page having a menu control , its not working properly.

Posted on 10/7/2011 1:57:30 AM by Bhavya #
Gravatar

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

Your code is very nice. Working Fine.Thank You.

Posted on 10/14/2011 3:25:29 AM by Lin #
Gravatar

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

Thanks for the help Prabakaran

Posted on 10/28/2011 12:16:05 AM by Vibhu #
Gravatar

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

How can i use this visual studio 2005

Posted on 11/23/2011 5:06:56 AM by Dorababu #
Gravatar

The same way if this is .NET 2.0 and up.

Gravatar

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

thank dear it's works thank's lot...

Posted on 12/7/2011 5:26:28 AM by Dheeraj Upadhyay #
Gravatar

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

How can change Title of message window???

Posted on 12/22/2011 7:00:09 AM by lalit #
Gravatar

It is not possible

Gravatar

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

hi..i want to know how to display ok cancel alert box

Posted on 12/23/2011 4:06:44 AM by Mini #
Gravatar

Use javascript confirm command.

Gravatar

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

thanxxx dear,,,, you have saved my tons of time.

Posted on 1/10/2012 3:58:46 AM by Sunil Kanojiya #
Gravatar

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

It works great. Thanks for your solution

Posted on 2/2/2012 1:19:53 AM by Yuva #
Gravatar

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

Excellent Post.
I was looking to show msgbox when user do not enter data..especially with ajax it works greate
Thanks Lot...

Posted on 2/4/2012 11:39:04 AM by Afridi #

New Comment

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

Categories

Recent Tweets

Valid HTML5