Call the JavaScript function from global.asax.cs

I want to call a JavaScript function (which internally displays an error popup) from the global.asax.cs file. Here is my code that I am trying to do in the Global.asax.cs file,

protected void Application_Error(object sender, EventArgs e) { System.Web.UI.Page mypage = (System.Web.UI.Page)HttpContext.Current.Handler; mypage.RegisterStartupScript("alert", "<script language=javascript>alert('test');</script>"); } 

But it does not raise a warning and does not give any error or warning messages in Firebug and the Google Chrome console. How can JavaScript code be called?

+4
source share
4 answers

At this point, you get two different types of errors.

  • Errors thrown by the compiler.
  • Errors that your program throws.

The compiler can throw any error there , for example, it cannot find a literal control in the code. In this case, your page does not even exist, and there is no way to get it at that moment and give this JavaScript warning.

Errors in the runtime of your program , such as a null exception, can also stop the page displaying, and this also stops the page, and you cannot have a page handler.

So, HttpContext.Current.Handler is NOT a page .

In general, Application_Error is used to capture and log unhandled errors , to resolve them later and possibly show the user a better error page.

If you try to see your error while debugging, you can use the code:

 void Application_Error(object sender, EventArgs e) { Exception LastOneError = Server.GetLastError(); if (LastOneError != null) { Debug.Fail("Unhandled error: " + LastOneError.ToString()); LogTheError(LastOneError); } } 

User Error Page

The only way I can do something like this is to redirect when an error appears on a new page explaining the error, but the custom error page already does that.

More details

This is how I handle my user errors and stay on the same page. Checking the file is to avoid a possible closed loop that could lead to a pool failure.

  string cTheFile = HttpContext.Current.Request.Path; if (!cTheFile.EndsWith("ErrorHandlePage.aspx")) Server.Transfer("~/ErrorHandlePage.aspx"); 
+3
source

This is written to the page and a message box appears. I put it in the global.asax.cs file (the code behind). Tested with division by zero error:

 void Application_Error(object sender, EventArgs e) { String ErrorStr = "Application_Error " + Server.GetLastError().Message; Response.Write("<h2>Global Page Error</h2>\n"); Response.Write("<p>" + ErrorStr + "</p>\n"); Response.Write("<Script Language='javascript'> alert('Hello');</script>"); // Clear the error from the server Server.ClearError(); } 
+1
source

The quick answer: you cannot.

There is no page in Global.asax, so there is no context in which javascript is run.

0
source

The MSDN Page.RegisterStartupScript Method (String, String) page contains an example of what you are trying to do.

I think your problem is that when you register a script on this page, it is too late. The life cycle of the page object is complete and it will not display any additional data on the page.

0
source

All Articles