@-quoted string in C#
-
Hello I am using
this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "") + "');");
to show errors on an aspx webpage which are caught using a try catch block. When the error message contains "\r\n" it throws an Unterminated String Constant error in javascript. I can resolve this quite easily usingthis.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "").Replace("\r\n", @"\n") + "');");
Does anyone know a more elegant solution to this though, as i presume "\n" etc. also might cause the same problem? Thanks -
Hello I am using
this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "") + "');");
to show errors on an aspx webpage which are caught using a try catch block. When the error message contains "\r\n" it throws an Unterminated String Constant error in javascript. I can resolve this quite easily usingthis.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "").Replace("\r\n", @"\n") + "');");
Does anyone know a more elegant solution to this though, as i presume "\n" etc. also might cause the same problem? ThanksEnvironment.NewLine ?
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
Environment.NewLine ?
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview ArchiveSorry I think you misunderstood me... for e.g. error message "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index" renders as: alert('Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'); It should render as: alert('Index was out of range. Must be non-negative and less than the size of the collection.\nParameter name: index'); so as not to throw a javascript error. I suppose I could use
this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "").Replace("\r", "").Replace("\n", @"\n") + "');");
Just thought there might be an easier solution... Thanks -
Sorry I think you misunderstood me... for e.g. error message "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index" renders as: alert('Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'); It should render as: alert('Index was out of range. Must be non-negative and less than the size of the collection.\nParameter name: index'); so as not to throw a javascript error. I suppose I could use
this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "").Replace("\r", "").Replace("\n", @"\n") + "');");
Just thought there might be an easier solution... ThanksUse
"\\n"
. That makes the first two slashes the escape sequence, and then
a plain character. -
Hello I am using
this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "") + "');");
to show errors on an aspx webpage which are caught using a try catch block. When the error message contains "\r\n" it throws an Unterminated String Constant error in javascript. I can resolve this quite easily usingthis.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("'", "").Replace("\r\n", @"\n") + "');");
Does anyone know a more elegant solution to this though, as i presume "\n" etc. also might cause the same problem? ThanksTo properly escape a string value that you put in a javascript string literal, you replace \ with \\ and ' with \'. Before that you can replace a line break with the escape code \n. And don't use the language attribute in the script tag. It's been deprecated for many years.
this.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('" + ex.Message.Replace("\r\n", @"\n").Replace(@"\", @"\\").Replace("'", @"\'") + "');");
Despite everything, the person most likely to be fooling you next is yourself.