I have make an method that i call when i cathc exceptions. But the alert is never displayed. I have tried with both the uncommented and commented code:
Put a breakpoint inside the method to make sure the code is getting called, and also to see what the exception value is. Perhaps exception has some characters that are caysing this trouble. To rule the latter issue out, you can hardcode alert('hi') instead.
There are 2 methods to call client side scripts (RegisterStartupScript and RegisterClientScriptBlock).
The difference between the two is RegisterStartupScript is called at the end of the Form load whereas RegisterClientScriptBlock is injected at the begining of the Form load.
So if you want to catch exceptions at the start you would need RegisterClientScript
Regards | Sudeep Kshirsagar | MCPD (ASP.Net) | MCTS(WCF) | My Blog Please mark the post as "Answer" if it helps you.
your code works for me perfectly...so you got to cross check if there are any javascript errors on page...or duplicate script key which is the third parameter in RegisterStartupScript()...
Put a breakpoint inside the method to make sure the code is getting called, and also to see what the exception value is. Perhaps exception has some characters that are caysing this trouble. To rule the latter issue out, you can hardcode alert('hi') instead.
If i just run with "hi" it work so it seem like it is something with the exception string. Is there a way to come around this? In this case i forced the code to exception ""Procedure or function 'usp_blabla' expects parameter '@p_bla', which was not supplied."
Ollza
Member
572 Points
304 Posts
ScriptManager.RegisterStartupScript
Feb 22, 2012 01:09 PM|LINK
Hi,
I have make an method that i call when i cathc exceptions. But the alert is never displayed. I have tried with both the uncommented and commented code:
protected void ExceptionAlert(string exception) { ScriptManager.RegisterStartupScript(Page, this.GetType(), "Exception", "alert('" + exception + "')", true); //ScriptManager.RegisterStartupScript(this, typeof(string), "Exception", "alert('" + // exception + "')", true); }MetalAsp.Net
All-Star
112732 Points
18369 Posts
Moderator
Re: ScriptManager.RegisterStartupScript
Feb 22, 2012 01:14 PM|LINK
Put a breakpoint inside the method to make sure the code is getting called, and also to see what the exception value is. Perhaps exception has some characters that are caysing this trouble. To rule the latter issue out, you can hardcode alert('hi') instead.
sudeep_13
Contributor
2549 Points
512 Posts
Re: ScriptManager.RegisterStartupScript
Feb 22, 2012 01:23 PM|LINK
Hi,
There are 2 methods to call client side scripts (RegisterStartupScript and RegisterClientScriptBlock).
The difference between the two is RegisterStartupScript is called at the end of the Form load whereas RegisterClientScriptBlock is injected at the begining of the Form load.
So if you want to catch exceptions at the start you would need RegisterClientScript
Please mark the post as "Answer" if it helps you.
ramiramilu
All-Star
97829 Points
14494 Posts
Re: ScriptManager.RegisterStartupScript
Feb 22, 2012 01:28 PM|LINK
your code works for me perfectly...so you got to cross check if there are any javascript errors on page...or duplicate script key which is the third parameter in RegisterStartupScript()...
follow code which works for me-
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { try { int j = 0; int i = 1 / j; } catch (Exception ex) { ExceptionAlert(ex.Message); } } protected void ExceptionAlert(string exception) { ScriptManager.RegisterStartupScript(Page, this.GetType(), "Exception", "alert('" + exception + "')", true); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:Button ID="Button1" runat="server" Text="Button"/> </div> </form> </body> </html>JumpStart
Ollza
Member
572 Points
304 Posts
Re: ScriptManager.RegisterStartupScript
Feb 22, 2012 01:39 PM|LINK
If i just run with "hi" it work so it seem like it is something with the exception string. Is there a way to come around this? In this case i forced the code to exception ""Procedure or function 'usp_blabla' expects parameter '@p_bla', which was not supplied."
MetalAsp.Net
All-Star
112732 Points
18369 Posts
Moderator
Re: ScriptManager.RegisterStartupScript
Feb 22, 2012 05:14 PM|LINK
nivvi
Member
68 Points
14 Posts
Re: ScriptManager.RegisterStartupScript
Feb 22, 2012 06:00 PM|LINK
Try this,
protected void ExceptionAlert(string exception)
{
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
{
script.RegisterClientScriptBlock(this.GetType(), "Exception", "alert('" + exception + "');", true);
}
}
hitesh.hirpa...
Member
64 Points
38 Posts
Re: ScriptManager.RegisterStartupScript
Feb 23, 2012 04:32 AM|LINK
put both alert bon same tag
bcoz its both not work at same time
ScriptManager.RegisterStartupScript(Page, this.GetType(), "Exception", "alert('" + exception + "');alert('" + exception + "')", true);Ollza
Member
572 Points
304 Posts
Re: ScriptManager.RegisterStartupScript
Feb 23, 2012 07:11 AM|LINK
Thnaks for all answers. HtmlEncode doesnt help. hitesh your code doesnt help either. Any other suggestions?
sandy060583
Star
8728 Points
1626 Posts
Re: ScriptManager.RegisterStartupScript
Feb 23, 2012 09:20 AM|LINK
Register the script during the PreRender phase.
Ramani Sandeep (My Blog)
(MCTS, MCC-2011)