Rizwan
Rizwan

Reputation: 109

alert not showing exception variable in asp.net

I want to show exception in alert box in asp.net 4. But it is not showing any alert. I tried these solutions -

try{// my code}
catch (Exception ex)
            {             
                //Response.Write("<script>alert('" + Server.HtmlEncode(ex.Message) + "')</script>"); // Not Working
                //Response.Write("<script>alert('" + ex.Message + "');</script>"); // Not working
       // Response.Write("<script>alert('" + Server.HtmlEncode(ex.ToString()) + "')</script>"); // Not working
//ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "done", "alert('Error Occured.');", true); // Only this is working.
            }

Please suggest any solution to this where I am going wrong.

Thanks and regards, Rizwan Gazi.

Upvotes: 1

Views: 1635

Answers (1)

Krunal Patil
Krunal Patil

Reputation: 3676

Normally Response.Write(); always works. But as you said its not working than

Try this :

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "done", "alert('" + ex.Message.Replace("'", "") + "');", true);

Let me know if it solves your issue.

Upvotes: 5

Related Questions