从 global.asax.cs 调用 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6776711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call JavaScript function from global.asax.cs
提问by mehul9595
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,
我想从 global.asax.cs 文件调用 JavaScript 函数(它在内部显示一个关于错误消息的弹出窗口)。这是我在 Global.asax.cs 文件中尝试的代码,
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 Firebugand the Google Chrome console. How can JavaScript code be called?
但它不会在Firebug和 Google Chrome 控制台中调用警报,也不会给出任何错误或警告消息。如何调用 JavaScript 代码?
采纳答案by Aristos
There are two different kind of errors that you get at that point.
此时您会遇到两种不同类型的错误。
- Errors that are thrown by the compiler.
- 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.
编译器可以在那里抛出任何错误,例如,它在后面的代码中找不到文字控件。在这种情况下,您的页面甚至不存在,此时无法获取它并发出此 JavaScript 警报。
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
.
所以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.
一般来说, Application_Error 用于捕获和记录unhandled errors
,以便稍后解决它们,并且可能向用户展示更好的错误页面。
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");
回答by Steve Wellens
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:
这将写入页面并弹出一个消息框。我把它放在 global.asax.cs 文件中(代码隐藏)。测试除以零错误:
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();
}
回答by Shannow
Quick answer is you can't.
快速回答是你不能。
There is no page in Global.asax so no context in which run javascript.
Global.asax 中没有页面,因此没有运行 javascript 的上下文。
回答by Brian Dishaw
The MSDN page Page.RegisterStartupScript Method (String,?String)has an example of what you are trying to do.
MSDN 页面Page.RegisterStartupScript Method (String,?String)有一个您正在尝试执行的操作的示例。
I think your problem is that when you are registering the script with that page, it's too late. The life cycle of the page object is over, and it's not going to render anything additional data to the page.
我认为您的问题是,当您在该页面上注册脚本时,为时已晚。页面对象的生命周期结束了,它不会向页面呈现任何额外的数据。