Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. @-quoted string in C#

@-quoted string in C#

Scheduled Pinned Locked Moved ASP.NET
helpcsharpjavascriptquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shahil Shah
    wrote on last edited by
    #1

    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 using this.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

    V G 2 Replies Last reply
    0
    • S Shahil Shah

      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 using this.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

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      Environment.NewLine ?

      V.
      Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

      S 1 Reply Last reply
      0
      • V V 0

        Environment.NewLine ?

        V.
        Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

        S Offline
        S Offline
        Shahil Shah
        wrote on last edited by
        #3

        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... Thanks

        A 1 Reply Last reply
        0
        • S Shahil Shah

          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... Thanks

          A Offline
          A Offline
          Adam Maras
          wrote on last edited by
          #4

          Use "\\n". That makes the first two slashes the escape sequence, and the n a plain character.

          1 Reply Last reply
          0
          • S Shahil Shah

            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 using this.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

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            To 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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups