Newline detected when add script in C# (ASP.NET)

Discussion in 'ASP .Net' started by Ivan Demkovitch, Nov 5, 2003.

  1. Hi!

    I'm trying to add script to the page and place following code in my ascx
    (control):


    Page.RegisterClientScriptBlock("adder", "<script>function
    doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
    eval(document.all.TextBox2.value)}</script>");

    It doesn't work giving me all kind of problems...

    I suspect the problem is with <> charachters.

    How do I go around this problem?

    Also, placing this line in code (I use WebMatrix) will mess up all HTML
    code??

    Thanks!
     
    Ivan Demkovitch, Nov 5, 2003
    #1
    1. Advertising

  2. Ivan Demkovitch

    Cliff Harris Guest

    If that is the exact line from your code, then it just has to do with how
    your formed your string. You cannot insert a line break in a string in C#.
    It should work if you change your code to read:

    Page.RegisterClientScriptBlock("adder", "<script>function " +
    "doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
    " +
    "eval(document.all.TextBox2.value)}</script>");

    or, for more readability, I always for my javascript in a temp variable
    first. Just kinda hard to read otherwise

    string script = "<script>function " +
    "doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
    " +
    "eval(document.all.TextBox2.value)}</script>"
    Page.RegisterClientScriptBlock("adder", script);

    either way, both of those should work for you.

    -Cliff


    "Ivan Demkovitch" <> wrote in message
    news:...
    > Hi!
    >
    > I'm trying to add script to the page and place following code in my ascx
    > (control):
    >
    >
    > Page.RegisterClientScriptBlock("adder", "<script>function
    > doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
    > eval(document.all.TextBox2.value)}</script>");
    >
    > It doesn't work giving me all kind of problems...
    >
    > I suspect the problem is with <> charachters.
    >
    > How do I go around this problem?
    >
    > Also, placing this line in code (I use WebMatrix) will mess up all HTML
    > code??
    >
    > Thanks!
    >
    >
     
    Cliff Harris, Nov 5, 2003
    #2
    1. Advertising

  3. I solved problem...

    Actually the problem was in "</script>"

    Changed to "<" + "/" + "script>" and it works fine now.

    Thanks!


    "Cliff Harris" <> wrote in message
    news:...
    > If that is the exact line from your code, then it just has to do with how
    > your formed your string. You cannot insert a line break in a string in

    C#.
    > It should work if you change your code to read:
    >
    > Page.RegisterClientScriptBlock("adder", "<script>function " +
    > "doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value)

    +
    > " +
    > "eval(document.all.TextBox2.value)}</script>");
    >
    > or, for more readability, I always for my javascript in a temp variable
    > first. Just kinda hard to read otherwise
    >
    > string script = "<script>function " +
    > "doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value)

    +
    > " +
    > "eval(document.all.TextBox2.value)}</script>"
    > Page.RegisterClientScriptBlock("adder", script);
    >
    > either way, both of those should work for you.
    >
    > -Cliff
    >
    >
    > "Ivan Demkovitch" <> wrote in message
    > news:...
    > > Hi!
    > >
    > > I'm trying to add script to the page and place following code in my ascx
    > > (control):
    > >
    > >
    > > Page.RegisterClientScriptBlock("adder", "<script>function
    > > doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
    > > eval(document.all.TextBox2.value)}</script>");
    > >
    > > It doesn't work giving me all kind of problems...
    > >
    > > I suspect the problem is with <> charachters.
    > >
    > > How do I go around this problem?
    > >
    > > Also, placing this line in code (I use WebMatrix) will mess up all HTML
    > > code??
    > >
    > > Thanks!
    > >
    > >

    >
    >
     
    Ivan Demkovitch, Nov 5, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Len
    Replies:
    3
    Views:
    1,461
  2. paul clayton via .NET 247
    Replies:
    2
    Views:
    11,102
    Fred Chateau
    Jun 28, 2004
  3. =?Utf-8?B?U3VtbWl0?=
    Replies:
    2
    Views:
    1,996
    =?Utf-8?B?U3VtbWl0?=
    Dec 15, 2004
  4. Replies:
    0
    Views:
    407
  5. Replies:
    1
    Views:
    388
    Lowell Heddings
    Dec 15, 2004
Loading...

Share This Page