c# - Call JavaScript function from global.asax.cs -


i want call javascript function (which internally shows popup error message) global.asax.cs file. here code 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 not calling alert nor giving error or warning message in firebug , google chrome console. how can javascript code called?

there 2 different kind of errors @ point.

  1. errors thrown compiler.
  2. errors thrown program.

the compiler can throw error there, example, can not find literal control in code behind. in case page not exist , there no way @ point , give javascript alert.

errors run time of program, example, null exception, can stop page rendered , stop page , can not have page handler there.

so httpcontext.current.handler not page.

in general, application_error used capture , log unhandled errors, solve them later, , maybe show better error page user.

if try see error on debug, 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 way can think make similar make redirect when error appears new page explain error - custom error page do.

more

this way handle custom errors , stay on same page. file check avoid possible close loop can crash pool.

  string cthefile = httpcontext.current.request.path;    if (!cthefile.endswith("errorhandlepage.aspx"))       server.transfer("~/errorhandlepage.aspx"); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -