ScriptManager.RegisterStartupScript(page,
page.GetType(),
"KEY",
"updateDockTitle('" + ClientID + "', '" + string.Format(format, ChartName, "No Data To Display") + "');".Replace("-", "\\-"),
true);
ChartName has the ability to contain special characters (e.g. Someone’s Chart). If allowed as is this causes an error the code hits the ' when it was not expecting it.
What’s the proper way to handle this scenario? Do I need to know all characters that could cause issues and replace them with escaped out versions?
I’ve found this comprehensive solution handy:
It should replace all invalid JS characters and also make sure there’s no breakage if your script is inline (which it would be in your case). Note that it already generates the quotes for the string literal, so keep that in mind if you decide to use it.