Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call JavaScript function from global.asax.cs

I want to call the JavaScript function (which internally shows a popup about the error message) from global.asax.cs file. Here is my code that I am trying in 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 is not calling the alert nor giving any error or warning message in Firebug and the Google Chrome console. How can JavaScript code be called?

like image 959
mehul9595 Avatar asked Jul 21 '11 13:07

mehul9595


2 Answers

There are two different kind of errors that you get at that point.

  1. Errors that are thrown by the compiler.
  2. Errors that are thrown by your program.

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

Errors by the run time of your program, for example, a null exception, can also stop the page from rendered and this is also stop the page and you can not have the page handler there.

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

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

If you try just to see your error on debug, you can use code like:

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

Custom error page

The only way that I can think to make something similar is to make a redirect when the error appears to a new page that explain the error - but this is what the custom error page already do.

More

This is the way I handle my custom errors and stay on the same page. The file check is to avoid possible close loop that can crash the pool.

  string cTheFile = HttpContext.Current.Request.Path;

  if (!cTheFile.EndsWith("ErrorHandlePage.aspx"))
      Server.Transfer("~/ErrorHandlePage.aspx");
like image 174
Aristos Avatar answered Oct 10 '22 13:10

Aristos


This writes to the page and pops up a message box. I put it in a global.asax.cs file (code behind). Tested with a divide 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();
}
like image 24
Steve Wellens Avatar answered Oct 10 '22 13:10

Steve Wellens