I have the following code in aspx file:-
<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:-
int l1 = int.Parse("Madhukar");
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:-
protected void Page_Error(object sender, EventArgs e)
In this method I handle this exception event using
Exception ex = Server.GetLastError();
I use the following code to get the error message:-
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:-
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"" + message + "\");", true);
The problem is it doesn't show a javascript alert. I even tried a simple :
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:-
Response.Write(message);
Response.Write("<script type='text/javascript'>alert('" + message + "');</script>");
They both work fine and show the javascript alert and write to the resulting webpage.
The final code is as follows:-
protected void Button1_Click(object sender, EventArgs e)
{
int l1 = int.Parse("Madhukar");
}
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);
}
Please help.
What I have tried:
I tried :-
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('hi msg from server');", true);
This also didn't show an alert popup.
I also tried
ClientScriptManager cs = Page.ClientScript;
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