Avoiding & when adding a JavaScript event handler using Attributes.Add()

Discussion in 'ASP .Net' started by Nathan Sokalski, Feb 28, 2006.

  1. I add a JavaScript event handler to some of my Webcontrols using the
    Attributes.Add() method as follows:


    Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    TextBox2.Attributes.Add("onKeyPress", jscode)


    You will notice that jscode contains the JavaScript Logical And operator
    (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
    output, even though it is intended to be client-side JavaScript, not a
    visible onscreen character. How can I get ASP.NET to output


    onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"

    instead of

    onKeyPress="return (event.keyCode>=65&amp;&amp;event.keyCode<=90);"


    I am using VB.NET to write my server-side code, and am using Visual Studio
    ..NET 2003 with .NET 1.1. Thanks in advance for any help you can give.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Feb 28, 2006
    #1
    1. Advertising

  2. RE: Avoiding &amp; when adding a JavaScript event handler using Attrib

    Abstractly speaking .innerHTML or .innerText might solve the problem (?)

    I just don’t know specifically where you can and/or want to try .innerText
    or .innerHTML.

    More of a general answer sorry I wish I had more time to look at it.



    "Nathan Sokalski" wrote:

    > I add a JavaScript event handler to some of my Webcontrols using the
    > Attributes.Add() method as follows:
    >
    >
    > Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    > TextBox2.Attributes.Add("onKeyPress", jscode)
    >
    >
    > You will notice that jscode contains the JavaScript Logical And operator
    > (&&). However, ASP.NET renders this as && in the code that is
    > output, even though it is intended to be client-side JavaScript, not a
    > visible onscreen character. How can I get ASP.NET to output
    >
    >
    > onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >
    > instead of
    >
    > onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >
    >
    > I am using VB.NET to write my server-side code, and am using Visual Studio
    > ..NET 2003 with .NET 1.1. Thanks in advance for any help you can give.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    >
    >
    =?Utf-8?B?U2Vhbg==?=, Feb 28, 2006
    #2
    1. Advertising

  3. Re: Avoiding &amp; when adding a JavaScript event handler using Attrib

    I don't think that either of those are part of the solution, because they
    refer to the stuff between the opening and closing tags, like the following:

    <HTMLTAG>innertext or innerhtml</HTMLTAG>

    My goal is to add an attribute to the tag, which would be like the
    following:

    <HTMLTAG ATTRIBUTE="ATTRIBUTEVALUE"></HTMLTAG>

    Using the Attributes.Add() method that I showed in my initial posting adds
    the attribute to the right place, but it replaces the ATTRIBUTEVALUE's &'s
    with &amp;'s, therefore causing the JavaScript to have incorrect syntax.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Sean" <> wrote in message
    news:...
    > Abstractly speaking .innerHTML or .innerText might solve the problem (?)
    >
    > I just don't know specifically where you can and/or want to try .innerText
    > or .innerHTML.
    >
    > More of a general answer sorry I wish I had more time to look at it.
    >
    >
    >
    > "Nathan Sokalski" wrote:
    >
    >> I add a JavaScript event handler to some of my Webcontrols using the
    >> Attributes.Add() method as follows:
    >>
    >>
    >> Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    >> TextBox2.Attributes.Add("onKeyPress", jscode)
    >>
    >>
    >> You will notice that jscode contains the JavaScript Logical And operator
    >> (&&). However, ASP.NET renders this as && in the code that is
    >> output, even though it is intended to be client-side JavaScript, not a
    >> visible onscreen character. How can I get ASP.NET to output
    >>
    >>
    >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >>
    >> instead of
    >>
    >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >>
    >>
    >> I am using VB.NET to write my server-side code, and am using Visual
    >> Studio
    >> ..NET 2003 with .NET 1.1. Thanks in advance for any help you can give.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>
    >>
    >>
    Nathan Sokalski, Feb 28, 2006
    #3
  4. Re: Avoiding &amp; when adding a JavaScript event handler using At

    Ah..I see, yeah I have no idea either. Its clear that you have a clear
    understanding what I was getting at and also how it doesnt apply to your
    situation.

    sorry about that.

    "Nathan Sokalski" wrote:

    > I don't think that either of those are part of the solution, because they
    > refer to the stuff between the opening and closing tags, like the following:
    >
    > <HTMLTAG>innertext or innerhtml</HTMLTAG>
    >
    > My goal is to add an attribute to the tag, which would be like the
    > following:
    >
    > <HTMLTAG ATTRIBUTE="ATTRIBUTEVALUE"></HTMLTAG>
    >
    > Using the Attributes.Add() method that I showed in my initial posting adds
    > the attribute to the right place, but it replaces the ATTRIBUTEVALUE's &'s
    > with &'s, therefore causing the JavaScript to have incorrect syntax.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Sean" <> wrote in message
    > news:...
    > > Abstractly speaking .innerHTML or .innerText might solve the problem (?)
    > >
    > > I just don't know specifically where you can and/or want to try .innerText
    > > or .innerHTML.
    > >
    > > More of a general answer sorry I wish I had more time to look at it.
    > >
    > >
    > >
    > > "Nathan Sokalski" wrote:
    > >
    > >> I add a JavaScript event handler to some of my Webcontrols using the
    > >> Attributes.Add() method as follows:
    > >>
    > >>
    > >> Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    > >> TextBox2.Attributes.Add("onKeyPress", jscode)
    > >>
    > >>
    > >> You will notice that jscode contains the JavaScript Logical And operator
    > >> (&&). However, ASP.NET renders this as && in the code that is
    > >> output, even though it is intended to be client-side JavaScript, not a
    > >> visible onscreen character. How can I get ASP.NET to output
    > >>
    > >>
    > >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    > >>
    > >> instead of
    > >>
    > >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    > >>
    > >>
    > >> I am using VB.NET to write my server-side code, and am using Visual
    > >> Studio
    > >> ..NET 2003 with .NET 1.1. Thanks in advance for any help you can give.
    > >> --
    > >> Nathan Sokalski
    > >>
    > >> http://www.nathansokalski.com/
    > >>
    > >>
    > >>

    >
    >
    >
    =?Utf-8?B?U2Vhbg==?=, Feb 28, 2006
    #4
  5. Re: Avoiding &amp; when adding a JavaScript event handler using At

    Well, I haven't found a way to do it using ONLY the Attributes.Add() method
    yet (if there is one, it might be unavoidable depending on how they coded
    the Add() method), but I think I figured out a workaround that I think will
    work for all situations. Rather than doing:


    Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    TextBox2.Attributes.Add("onKeyPress", jscode)

    I will do the following:

    Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    Page.RegisterClientScriptBlock("TextBox2_RestrictInput", "<script
    type=""text/javascript"">function TextBox2_RestrictInput(){" & jscode &
    "}</script>")
    TextBox2.Attributes.Add("onKeyPress", "return TextBox2_RestrictInput();")


    This basically writes my JavaScript code as a function, and then I just
    return the value returned by the function. Apparently
    RegisterClientScriptBlock leaves the &'s alone, so it works. Maybe this is
    how ASP.NET intended all generated JavaScript to be added, although I think
    it would be nice to be able to do it without the RegisterClientScriptBlock
    method, but I guess I'll have to deal with it this way for now.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Sean" <> wrote in message
    news:...
    > Ah..I see, yeah I have no idea either. Its clear that you have a clear
    > understanding what I was getting at and also how it doesnt apply to your
    > situation.
    >
    > sorry about that.
    >
    > "Nathan Sokalski" wrote:
    >
    >> I don't think that either of those are part of the solution, because they
    >> refer to the stuff between the opening and closing tags, like the
    >> following:
    >>
    >> <HTMLTAG>innertext or innerhtml</HTMLTAG>
    >>
    >> My goal is to add an attribute to the tag, which would be like the
    >> following:
    >>
    >> <HTMLTAG ATTRIBUTE="ATTRIBUTEVALUE"></HTMLTAG>
    >>
    >> Using the Attributes.Add() method that I showed in my initial posting
    >> adds
    >> the attribute to the right place, but it replaces the ATTRIBUTEVALUE's
    >> &'s
    >> with &'s, therefore causing the JavaScript to have incorrect syntax.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>
    >> "Sean" <> wrote in message
    >> news:...
    >> > Abstractly speaking .innerHTML or .innerText might solve the problem
    >> > (?)
    >> >
    >> > I just don't know specifically where you can and/or want to try
    >> > .innerText
    >> > or .innerHTML.
    >> >
    >> > More of a general answer sorry I wish I had more time to look at it.
    >> >
    >> >
    >> >
    >> > "Nathan Sokalski" wrote:
    >> >
    >> >> I add a JavaScript event handler to some of my Webcontrols using the
    >> >> Attributes.Add() method as follows:
    >> >>
    >> >>
    >> >> Dim jscode as String = "return
    >> >> (event.keyCode>=65&&event.keyCode<=90);"
    >> >> TextBox2.Attributes.Add("onKeyPress", jscode)
    >> >>
    >> >>
    >> >> You will notice that jscode contains the JavaScript Logical And
    >> >> operator
    >> >> (&&). However, ASP.NET renders this as && in the code that is
    >> >> output, even though it is intended to be client-side JavaScript, not a
    >> >> visible onscreen character. How can I get ASP.NET to output
    >> >>
    >> >>
    >> >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >> >>
    >> >> instead of
    >> >>
    >> >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >> >>
    >> >>
    >> >> I am using VB.NET to write my server-side code, and am using Visual
    >> >> Studio
    >> >> ..NET 2003 with .NET 1.1. Thanks in advance for any help you can give.
    >> >> --
    >> >> Nathan Sokalski
    >> >>
    >> >> http://www.nathansokalski.com/
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
    Nathan Sokalski, Mar 1, 2006
    #5
  6. Nathan Sokalski

    Scott M. Guest

    Server.HTMLDecode("&amp;&amp;")


    "Nathan Sokalski" <> wrote in message
    news:%23O%...
    >I add a JavaScript event handler to some of my Webcontrols using the
    >Attributes.Add() method as follows:
    >
    >
    > Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    > TextBox2.Attributes.Add("onKeyPress", jscode)
    >
    >
    > You will notice that jscode contains the JavaScript Logical And operator
    > (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
    > output, even though it is intended to be client-side JavaScript, not a
    > visible onscreen character. How can I get ASP.NET to output
    >
    >
    > onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >
    > instead of
    >
    > onKeyPress="return (event.keyCode>=65&amp;&amp;event.keyCode<=90);"
    >
    >
    > I am using VB.NET to write my server-side code, and am using Visual Studio
    > .NET 2003 with .NET 1.1. Thanks in advance for any help you can give.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Scott M., Mar 1, 2006
    #6
  7. "Nathan Sokalski" <> wrote in
    news:#O#:

    > I add a JavaScript event handler to some of my Webcontrols using
    > the Attributes.Add() method as follows:
    >
    >
    > Dim jscode as String = "return
    > (event.keyCode>=65&&event.keyCode<=90);"
    > TextBox2.Attributes.Add("onKeyPress", jscode)
    >
    >
    > You will notice that jscode contains the JavaScript Logical And
    > operator (&&). However, ASP.NET renders this as &amp;&amp; in
    > the code that is output, even though it is intended to be
    > client-side JavaScript, not a visible onscreen character. How
    > can I get ASP.NET to output
    >
    >
    > onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >
    > instead of
    >
    > onKeyPress="return
    > (event.keyCode>=65&amp;&amp;event.keyCode<=90);"
    >
    >
    > I am using VB.NET to write my server-side code, and am using
    > Visual Studio .NET 2003 with .NET 1.1. Thanks in advance for any
    > help you can give.


    Nathan,

    I think your solution of using Page.RegisterClientScriptBlock is
    about the only reasonable thing that will work.

    I used Reflector to trace thru
    System.Web.UI.WebControlsWebControl.Attributes to see if it was doing
    any kind of encoding. It turns out that
    System.Web.HttpUtility.HtmlAttributeEncode is called on any text
    added via Attributes.Add. As you've found out, HtmlAttributeEncode
    turns & into &amp;. It also turns double quotes (") into &quot;.

    --
    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Mar 1, 2006
    #7
  8. That would return the String && which is what I am currently putting in. The
    conversion to &amp;&amp; occurs somewhere between the call to
    Attributes.Add() and the time that the page is sent to the browser. The
    problem is that I don't know where, and if I did, I am not sure it is an
    area of code that I have the ability to edit, because I think it is in some
    area of the code that is part of ASP.NET, and I therefore do not have access
    to the source code. I do still want to find a way to output characters
    without having them go through what I think is the Server.HtmlEncode()
    method, but I have found a reasonably simple workaround for doing what I was
    trying to do that caused me to start this thread (see the message I posted
    at 2/28/2006 7:32 PM for the workaround I found)
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Scott M." <> wrote in message
    news:...
    > Server.HTMLDecode("&amp;&amp;")
    >
    >
    > "Nathan Sokalski" <> wrote in message
    > news:%23O%...
    >>I add a JavaScript event handler to some of my Webcontrols using the
    >>Attributes.Add() method as follows:
    >>
    >>
    >> Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    >> TextBox2.Attributes.Add("onKeyPress", jscode)
    >>
    >>
    >> You will notice that jscode contains the JavaScript Logical And operator
    >> (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
    >> output, even though it is intended to be client-side JavaScript, not a
    >> visible onscreen character. How can I get ASP.NET to output
    >>
    >>
    >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    >>
    >> instead of
    >>
    >> onKeyPress="return (event.keyCode>=65&amp;&amp;event.keyCode<=90);"
    >>
    >>
    >> I am using VB.NET to write my server-side code, and am using Visual
    >> Studio .NET 2003 with .NET 1.1. Thanks in advance for any help you can
    >> give.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, Mar 1, 2006
    #8
  9. Nathan Sokalski

    Raymond Guest

    Yup, I don't think there is any easy solution save the
    workaround you used. Frankly, I think they got this
    one wrong in ASP.NET. It does the html encoding
    automatically on that method call. The only other
    workaround I can think of is to use methods of the HtmlTextWriter
    and simply write the whole element without the
    encoding turned on. That's not as easy to pull off as your workaround
    and may need a custom control.

    The correct way for ASP.NET to have done this would've been to at least
    turn off html encoding when "javascript:" is prepended to the text, or
    better
    yet, another parameter in that method for the encoding, just like one of the
    overloads of the AddAttribute method of the HtmlTextWriter.



    "Nathan Sokalski" <> wrote in message
    news:...
    > That would return the String && which is what I am currently putting in.

    The
    > conversion to &amp;&amp; occurs somewhere between the call to
    > Attributes.Add() and the time that the page is sent to the browser. The
    > problem is that I don't know where, and if I did, I am not sure it is an
    > area of code that I have the ability to edit, because I think it is in

    some
    > area of the code that is part of ASP.NET, and I therefore do not have

    access
    > to the source code. I do still want to find a way to output characters
    > without having them go through what I think is the Server.HtmlEncode()
    > method, but I have found a reasonably simple workaround for doing what I

    was
    > trying to do that caused me to start this thread (see the message I posted
    > at 2/28/2006 7:32 PM for the workaround I found)
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Scott M." <> wrote in message
    > news:...
    > > Server.HTMLDecode("&amp;&amp;")
    > >
    > >
    > > "Nathan Sokalski" <> wrote in message
    > > news:%23O%...
    > >>I add a JavaScript event handler to some of my Webcontrols using the
    > >>Attributes.Add() method as follows:
    > >>
    > >>
    > >> Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
    > >> TextBox2.Attributes.Add("onKeyPress", jscode)
    > >>
    > >>
    > >> You will notice that jscode contains the JavaScript Logical And

    operator
    > >> (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
    > >> output, even though it is intended to be client-side JavaScript, not a
    > >> visible onscreen character. How can I get ASP.NET to output
    > >>
    > >>
    > >> onKeyPress="return (event.keyCode>=65&&event.keyCode<=90);"
    > >>
    > >> instead of
    > >>
    > >> onKeyPress="return (event.keyCode>=65&amp;&amp;event.keyCode<=90);"
    > >>
    > >>
    > >> I am using VB.NET to write my server-side code, and am using Visual
    > >> Studio .NET 2003 with .NET 1.1. Thanks in advance for any help you can
    > >> give.
    > >> --
    > >> Nathan Sokalski
    > >>
    > >> http://www.nathansokalski.com/
    > >>

    > >
    > >

    >
    >
    Raymond, Mar 1, 2006
    #9
    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. Laurent Bugnion
    Replies:
    1
    Views:
    10,016
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 21, 2006
  2. Nathan Sokalski
    Replies:
    11
    Views:
    802
    Alessandro Zifiglio
    Jul 27, 2006
  3. Replies:
    1
    Views:
    682
    Damien
    Feb 22, 2007
  4. RC

    How to recall add event from an Event handler??

    RC, Jan 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    246
    John Saunders
    Jan 6, 2005
  5. Une bévue
    Replies:
    2
    Views:
    84
    Une bévue
    Jan 26, 2006
Loading...

Share This Page