Using a javascript alert box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    #1

    Using a javascript alert box

    I am beginning to write some C# pages to an app I created with VB.Net.
    There is one VB routine I use frequently - a javascript alert box. I have
    created a C# version, but it does not handle newlines. I get an
    "unterminat ed string constant" error message. Googling this problem gives
    some hints that there are others with this problem, but I could not find a
    solution.

    In addition to this method, I tried the RegisterClientS criptBlock way, but
    get the same results. I have also tried several ways of building the string
    (enclosing with single quotes & double quotes, the ToString() method, etc.)
    without success. I should add that I can use the C# routine without
    incident if I pass a normal string variable. It's only when I pass in a
    newline character (and I suspect other escaped characters) that the problem
    occurs.

    I am enclosing some code below. If somebody could help me out with this
    problem, I would greatly appreciate it.

    First VB.Net:

    Public Sub DisplayAlert(By Val msg As String)
    response.write( "<script language=JavaSc ript> alert('" & msg & "'); <" &
    "/script>")
    end sub

    Calling this routine the following way gives the expected result:

    dim s as string = "Line 1\n\rLine 2"
    DisplayAlert( s )


    Result:

    Line 1
    Line 2

    ----------------------------------------------------------------
    ----------------------------------------------------------------

    Now in C#:

    private void DisplayAlert(st ring msg)
    {
    Response.Write( "<script language=JavaSc ript> alert('" + msg + "'); <" +
    "/script>");
    }

    Calling this routine the following way gives the 'unterminated string
    constant' error.

    string s = "Line 1\nLine 2";
    DisplayAlert( s );


    Can anyone help me?

    Thanks,

    Dave


  • Jon Skeet [C# MVP]

    #2
    Re: Using a javascript alert box

    Dave <dave_Please_re ply_to_group_on ly@stic.net> wrote:

    <snip>
    [color=blue]
    > Now in C#:
    >
    > private void DisplayAlert(st ring msg)
    > {
    > Response.Write( "<script language=JavaSc ript> alert('" + msg + "'); <" +
    > "/script>");
    > }
    >
    > Calling this routine the following way gives the 'unterminated string
    > constant' error.
    >
    > string s = "Line 1\nLine 2";
    > DisplayAlert( s );
    >
    >
    > Can anyone help me?[/color]

    The problem is that you want the string to be literally
    "Line 1\nLine2" whereas the code you've written makes it actually:

    "Line 1
    Line 2"

    (i.e. with a newline in it). You'll need to escape the backslash:

    string s = "Line 1\\nLine 2";

    That should do the trick.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Dave

      #3
      Re: Using a javascript alert box - SOLVED

      You nailed it. Almost embarassed to say how long I spent on this.

      Thanks a lot, Jon.

      Dave



      [snip][color=blue]
      > (i.e. with a newline in it). You'll need to escape the backslash:
      >
      > string s = "Line 1\\nLine 2";
      >
      > That should do the trick.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Hans Kesting

        #4
        Re: Using a javascript alert box

        >[color=blue]
        > private void DisplayAlert(st ring msg)
        > {
        > Response.Write( "<script language=JavaSc ript> alert('" + msg + "'); <" +
        > "/script>");
        > }
        >[/color]

        Note: you don't need to "break the </script> tag" as in old ASP.
        Codebehind sees nothing special in that string. In fact, I suspect the
        compiler just joins the two (constant) parts.

        Hans Kesting


        Comment

        • Dave

          #5
          Re: Using a javascript alert box

          Thank you for that insight, Hans. I develop in an environment where all the
          apps are inline, not code-behind, and don't have any say in that issue. If
          I change it to this -

          Response.Write( "<script language=JavaSc ript> alert('" + msg + "');
          </script>");

          I get the old 'CS1010: Newline in constant' error.

          Nevertheless, I'm glad you pointed that out. Breaking up that tag is
          inelegant at best, and I'm sure I'll put your pointer to use some time in
          the future.

          Dave


          "Hans Kesting" <news.2.hansdk@ spamgourmet.com > wrote in message
          news:mn.7b1e7d6 251b22449.43821 @spamgourmet.co m...[color=blue][color=green]
          > >
          >> private void DisplayAlert(st ring msg)
          >> {
          >> Response.Write( "<script language=JavaSc ript> alert('" + msg + "'); <" +
          >> "/script>");
          >> }
          >>[/color]
          >
          > Note: you don't need to "break the </script> tag" as in old ASP.
          > Codebehind sees nothing special in that string. In fact, I suspect the
          > compiler just joins the two (constant) parts.
          >
          > Hans Kesting
          >
          >[/color]


          Comment

          Working...