Click here to Skip to main content
15,891,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following code in aspx file:-
ASP.NET
<form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>

In the code behind I am trying to run a javascript alert by using ClientScript.RegisterStartupScript.
The logic of my program is that I first create an unhandled exception in the button click event as follows:-
C#
int l1 = int.Parse("Madhukar"); // This causes exception

and then that exception is propogated to page level automatically as it is unhandled and is automatically handled by the following page level error handler method:-
C#
protected void Page_Error(object sender, EventArgs e)

In this method I handle this exception event using
C#
Exception ex = Server.GetLastError(); // This gets the latest exception in the program.

I use the following code to get the error message:-
C#
string message = string.Format("At Page Level : Message: {0}\\n\\n", ex.Message);
        message += string.Format("StackTrace: {0}\\n\\n", ex.StackTrace.Replace(Environment.NewLine, 

string.Empty));
        message += string.Format("Source: {0}\\n\\n", ex.Source.Replace(Environment.NewLine, 

string.Empty));
        message += string.Format("TargetSite: {0}\\n\\n", ex.TargetSite.ToString().Replace

(Environment.NewLine, string.Empty));

Then I try to show a message in a javascript alert message using the following code:-
C#
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"" + message + "\");", true);

The problem is it doesn't show a javascript alert. I even tried a simple :
C#
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('hi msg from server');", true);

This also didn't show an alert popup.

I used the following code also:-
C#
Response.Write(message);  //This works
Response.Write("<script type='text/javascript'>alert('" + message + "');</script>"); // This also works.

They both work fine and show the javascript alert and write to the resulting webpage.

The final code is as follows:-
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        int l1 = int.Parse("Madhukar"); // This causes unhandled exception
    }


 protected void Page_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();

string message = string.Format("At Page Level : Message: {0}\\n\\n", ex.Message);
        message += string.Format("StackTrace: {0}\\n\\n", ex.StackTrace.Replace(Environment.NewLine, 

string.Empty));
        message += string.Format("Source: {0}\\n\\n", ex.Source.Replace(Environment.NewLine, 

string.Empty));
        message += string.Format("TargetSite: {0}\\n\\n", ex.TargetSite.ToString().Replace

(Environment.NewLine, string.Empty));

ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"" + message + "\");", true); 

//Show the javascript pop up alert.

}

Please help.

What I have tried:

I tried :-
C#
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('hi msg from server');", true);
This also didn't show an alert popup.

I also tried
C#
ClientScriptManager cs = Page.ClientScript;

        // Check to see if the startup script is already registered.
        if (!cs.IsStartupScriptRegistered(this.GetType(), "alert"))
        {
            cs.RegisterClientScriptBlock(this.GetType(), "alert", "alert('hi msg from server');", true);
        }

this also doesn't work.

I also tried :-
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "", script, true);

This also doesn't work
Posted
Updated 30-Oct-16 3:59am
v3
Comments
F-ES Sitecore 31-Oct-16 4:55am    
How does the page appear on error? Does it show the standard error page, or a custom error page?
Karthik_Mahalingam 3-Nov-16 2:29am    
are you using update panel?

1 solution

It is all about page-life-cycle...To RegisterStartupScript to work the life-cycle must be completed, but the error (by the nature of the try-catch error handling) breaks it...
So the only way to render something to the same page is by directly calling the Response.Write method...
---
However. If all you want is to show a JavaScript alert, than do all the code directly in the OnClick event handler - from there the RegisterStartupScript will work for you and no need to threw an exception for nothing (exceptions are cost a lot)...
 
Share this answer
 
v2
Comments
Madhukar Krishna 30-Oct-16 11:17am    
Thanks for the answer. I already know what you have said. I know exceptions are costly and slow up the website, but I was experimenting with exception handling at different levels like try-catch(that works), page level in Page_Error method( that doesn't work) and I am yet to try application level in Application_Error method in Global.asax file.
The request.response works in all levels. But is there any way that I can get ClientScript.RegisterStartupScript in page_error method? Please any help will be appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900