ScriptManager.RegisterStartupScript using JavaScriptSerializer - To display the Alert or Message Box from Code behind

| Thursday, March 13, 2014
While registering a message using the javascript code in RegisterStartupScript, message might contain some escape characters like ' or " this will definitely break the javascript. Hence we need to encode it, for example using JavaScriptSerializer as follows.

  string message = new JavaScriptSerializer().Serialize(ex.Message.ToString());  
  string myScript = String.Format("alert('{0}');", msg);  
  ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);  


Post a Comment

Next Prev