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 (49) | 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 #
Gravatar

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

Good one,
Am searching for long time..
Thanks

MH,IN

Posted on 2/6/2012 10:13:12 AM by K.Bala #
Gravatar

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

it works after the page design completely loaded...
but my goal is to check whether the session is null or not then the want to show alert and then redirect...

but this soln...shows design in backgroud when it is showing alert...

Posted on 3/14/2012 10:00:26 AM by kalpesh thakor #
Gravatar

You need to use AJAX for this. Try jQuery ajax it is the easiest way.

Gravatar

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

This is excellent. It really helped me.

Posted on 3/27/2012 6:14:10 AM by Lakshmi #
Gravatar

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

Excellent. It works fine.

Posted on 5/10/2012 7:12:47 AM by Harshal Yelpale #
Gravatar

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

Really nice...
keep Helping to all...

Posted on 6/1/2012 11:03:10 PM by Sanjay #
Gravatar

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

Excellent. It works fine. Ty very much

Posted on 8/1/2012 6:21:23 AM by Chris #
Gravatar

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

just to thanks. It's really helpful

Posted on 8/13/2012 1:15:41 AM by Duff #
Gravatar

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

Nice, I really help it. Thanks.

Posted on 8/26/2012 10:37:15 PM by Tanaji Bhanvase #
Gravatar

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

Thanks. It works but I have a problem here.
I have a image in my webpage, when i call the ShowAlertMessage method the alert box will appear but my image will disappear and after i click on 'Ok' it will appear again. How to make the image not disappear?

Thanks

Posted on 9/17/2012 2:20:08 AM by alvin #
Gravatar

Based on your description it is hard to tell what's wrong with your page. Do you use javascript to initialize your image source? May be this initialization script runs after ShowAlertMessage. Also it might be a css issue. Make sure that there is enough space for the image. I would assign overflow:hidden to the image container.

Gravatar

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

How to break a line?

The below doesn't work:
Dim cstext As String ="alert('" & "ddd".Replace(vbLf, "") & "xxx".Replace(vbLf, "") & "')"

It displayed dddxxx but I want

ddd

xxx

Posted on 10/5/2012 1:45:27 AM by john lim #
Gravatar

Dim cstext As String ="alert('" & "ddd".Replace(vbLf, " ") & "xxx".Replace(vbLf, " ") & "')"
or
Dim cstext As String ="alert('" & "ddd".Replace(vbLf, "\n") & "xxx".Replace(vbLf, "\n") & "')"

Gravatar

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

Thanks very useful, but I have some other different requirement that can I use the same code in asmx web service in catch block to see what error is thrown by the service?
Please do reply.

Posted on 10/9/2012 4:35:54 AM by Ganesh #
Gravatar

It depends how you are using this webservice. Webservice doesn't have any connection to UI by itself. You can pass back error to webservice caller and then display error from there.

Gravatar

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

Dear Viktar,

Sorry for the late update! I just return to work!

Thanks for the advice, for some reason, it didn't work (still display everything in the same line)

The below is my complete code:

Dim csname As String = "PopupScript"
Dim cstype As Type = Me.GetType()
Dim cs As ClientScriptManager = Page.ClientScript
Dim cstext As String = "alert('" & "First Line".Replace(vbCr, "\n") & "Second Line".Replace(vbCr, "\n") & "')"
cs.RegisterStartupScript(cstype, csname, cstext, True)

TIA
John

Posted on 10/15/2012 2:20:02 AM by john lim #
Gravatar

I just tested new line works in IE7+,Firefox and Chrome. Your replace probably doesn't work. Try following:

Dim cstext As String = "alert('" & "First Line".Replace(vbCrLf, "\n") & "Second Line".Replace(vbCrLf, "\n") & "')"

or

Dim cstext As String = "alert('" & "First Line".Replace(vbLf, "\n") & "Second Line".Replace(vbLf, "\n") & "')"

New Comment

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

Categories

Valid HTML5