Home All Groups Group Topic Archive Search About

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

Author
28 Feb 2006 10:26 PM
Nathan Sokalski
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
njsokal***@hotmail.com
http://www.nathansokalski.com/

Author
1 Mar 2006 3:18 AM
Scott M.
Server.HTMLDecode("&amp;&amp;")


Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:%23O%23qAYLPGHA.2088@tk2msftngp13.phx.gbl...
>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
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
Author
1 Mar 2006 4:31 AM
Nathan Sokalski
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
njsokal***@hotmail.com
http://www.nathansokalski.com/

Show quoteHide quote
"Scott M." <s-mar@nospam.nospam> wrote in message
news:e2VK66NPGHA.1312@TK2MSFTNGP09.phx.gbl...
> Server.HTMLDecode("&amp;&amp;")
>
>
> "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
> news:%23O%23qAYLPGHA.2088@tk2msftngp13.phx.gbl...
>>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
>> njsokal***@hotmail.com
>> http://www.nathansokalski.com/
>>
>
>
Author
1 Mar 2006 5:08 AM
Raymond
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.



Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:OMzPOkOPGHA.2036@TK2MSFTNGP14.phx.gbl...
> 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
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:e2VK66NPGHA.1312@TK2MSFTNGP09.phx.gbl...
> > Server.HTMLDecode("&amp;&amp;")
> >
> >
> > "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
> > news:%23O%23qAYLPGHA.2088@tk2msftngp13.phx.gbl...
> >>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
> >> njsokal***@hotmail.com
> >> http://www.nathansokalski.com/
> >>
> >
> >
>
>
Author
1 Mar 2006 4:26 AM
Chris R. Timmons
Show quote Hide quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in
news:#O#qAYLPGHA.2088@tk2msftngp13.phx.gbl:

> 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/