473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
nj********@hotmail.com
http://www.nathansokalski.com/
Feb 28 '06 #1
8 2761
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
nj********@hotmail.com
http://www.nathansokalski.com/

Feb 28 '06 #2
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Sean" <Se**@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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
nj********@hotmail.com
http://www.nathansokalski.com/

Feb 28 '06 #3
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Sean" <Se**@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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
nj********@hotmail.com
http://www.nathansokalski.com/


Feb 28 '06 #4
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_RestrictI nput", "<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
nj********@hotmail.com
http://www.nathansokalski.com/

"Sean" <Se**@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Sean" <Se**@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
> 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
>> nj********@hotmail.com
>> http://www.nathansokalski.com/
>>
>>
>>


Mar 1 '06 #5
Server.HTMLDecode("&amp;&amp;")
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2******************@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
nj********@hotmail.com
http://www.nathansokalski.com/

Mar 1 '06 #6
"Nathan Sokalski" <nj********@hotmail.com> wrote in
news:#O**************@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/
Mar 1 '06 #7
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Scott M." <s-***@nospam.nospam> wrote in message
news:e2**************@TK2MSFTNGP09.phx.gbl...
Server.HTMLDecode("&amp;&amp;")
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2******************@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
nj********@hotmail.com
http://www.nathansokalski.com/


Mar 1 '06 #8
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" <nj********@hotmail.com> wrote in message
news:OM**************@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
nj********@hotmail.com
http://www.nathansokalski.com/

"Scott M." <s-***@nospam.nospam> wrote in message
news:e2**************@TK2MSFTNGP09.phx.gbl...
Server.HTMLDecode("&amp;&amp;")
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2******************@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
nj********@hotmail.com
http://www.nathansokalski.com/



Mar 1 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Prodip Saha | last post by:
Dear ASP.NET Gurus, I have a TextBox control with AutoPostBack set to true to execute the server scripts. I also, added some client script for validation.What I want is--execute the client script...
2
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
3
by: Nathan Sokalski | last post by:
I have a VB.NET function that I am using in an ASP.NET page. The code creates a String, which contains && (the JavaScript Logical AND operator) and is used as part of the JavaScript sent to the...
8
by: Nathan Sokalski | last post by:
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);"...
13
by: rincewind | last post by:
I remember reading an article (was it Herb Sutter's?) that recommended avoiding using directives. While I quite understand this recommendation for headers, what's wrong in using directive in .cpp...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
3
by: Nathan Sokalski | last post by:
I am adding an onmouseover attribute using the Attributes.Add() method, and the String I am using for the value contains the & character. However, when rendered the & is converted to the HTML...
16
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi All, I have a neat little script that calculates price based on quantity without refreshing the page.. the script is - <script type="text/javascript"> function OpenWin(url) {...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.