I am trying to show exception message through javascript alert box.
Here is the sample code.
public static void HandleException(Page page, Exception ex)
{
string message = ex.Message.ToString();
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "", "alert('"+message+"');", true);
}
It runs if i give literal values for the string variable.
e.g.
string message = "Hello World";
But it fails if I give message = ex.Message;
Any Idea?
You need to encode it, for example using JavaScriptSerializer because if the message contains some escape characters like
'or"this will definitely break your javascript: