473,394 Members | 1,845 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.

how do i stop a code block HtmlEncoding characters??

hi,
i have a group of individual radio buttons that i am running some
client-side code on for the onClick event. there is a URL generated by
a code block inside the onClick attribute. the problem is that when i
view-source on the page, .net has html-encoded the quote and ampersand
characters that are in the code block into " and &. i don't
want this to happen. they should stay as literal characters.

<asp:RadioButton onClick='<%#
"javascript:Record(\"../Select.asmx/EnterPreference?ID=" +
DataBinder.Eval(Container.DataItem, "Proposal Nbr").ToString() +
"&pref=3\")" %>' runat="server" Text="3rd Preference"
GroupName="Select1"></asp:RadioButton>

note that the characters in question are the opening and closing quotes
around the one parameter for the javascript Record function, and the
ampersand which is part of the querystring in the url. the javascript
function actually still works, but it is bad markup as far as i'm
concerned and i'd say older browsers wouldn't accept it.

i was able to work around the " character, by inserting a (char)39
instead of the literal quote \" but unfortunately (char)38 has no
effect on the ampersand, it still comes out as &amp;

how can i stop this behaviour? also, i would be interested to know why
..Net behaves like this. normally if i want this to happen, i use
HttpUtility.HtmlEncode, but for some reason this is happening behind
the scenes.

thanks in advance for any help
tim

Nov 19 '05 #1
8 2594
Hi Tim_Mac,

Welcome to ASPNET newsgroup.
As for the encoding problem you mentioned, here are some of my
understanding:

The databinding code we put in the <%# ....%> block reply on the
programming language we used for developing the page, C# or VB.NET.... So
what's your page's language?

Based on my local test, I can successfully emebed the URL string with begin
and end quote into the page through databiding block , below are the code
snipeet I used( C# and VB.NET):

"PageUrl" is a public Page member(string type)
(PageUrl="mainpage.aspx?name=myname&class=myclass" )
[c#]
============
<script language="jscript">
function Record(str)
{
alert(str);
}
</script>
....................
<INPUT type="button" value="Button"
onclick='<%# "Record(\"./" + PageUrl + "\")" %>'>

===============
C# use \" to escape ".
[vb.net]
============
<script language="jscript">
function Record(str)
{
alert(str);
}
</script>
..................
<INPUT type="button" value="Button" id="btn"
onclick='<%# "Record(""./" & PageUrl & """)" %>'/>
=====================

Since VB.NET use "" to escape ".

Both one will output
"./mainpage.aspx?name=myname&class=myclass"

Please have a try to see whether the above also works on your side. If
there're anything different or not clear, please feel free to post here.
Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #2

Tim_Mac wrote:
hi,
i have a group of individual radio buttons that i am running some
client-side code on for the onClick event. there is a URL generated by a code block inside the onClick attribute. the problem is that when i view-source on the page, .net has html-encoded the quote and ampersand characters that are in the code block into &quot; and &amp;. i don't
want this to happen. they should stay as literal characters.

<asp:RadioButton onClick='<%#
"javascript:Record(\"../Select.asmx/EnterPreference?ID=" +
DataBinder.Eval(Container.DataItem, "Proposal Nbr").ToString() +
"&pref=3\")" %>' runat="server" Text="3rd Preference"
GroupName="Select1"></asp:RadioButton>

Okay, I don't know why you're getting the problem you've described, but
as a workaround have you considered:

<asp:RadioButton
onClick='javascript:Record(\"../Select.asmx/EnterPreference?ID=<%#
DataBinder.Eval(Container.DataItem, "Proposal
Nbr").ToString()%>&pref=3\")' runat="server" Text="3rd Preference"
GroupName="Select1"></asp:RadioButton>

which keeps all of the special characters outside of your code block?

Damien

Nov 19 '05 #3
hi Steven
thanks for the reply. your example works, but that is because it's a
html control, and there appears to be inconsistent behaviour for code
blocks in html controls and server controls.
if you change your input button to an asp:radiobutton, you should see
what i am talking about.

if it's a html input button, the html output is:
onclick='alert("./mainpage.aspx?name=myname&class=myclass")'
which is the correct output.

but if its an asp:radiobutton with an identical onclick event, the
source output is:
onclick="alert(&quot;./mainpage.aspx?name=myname&amp;class=myclass&quot;) "
which i am saying is not correct, because i never asked it to
htmlencode the " and & characters.

hopefully you can reproduce this at your end so you know what i'm on
about. the aspx source for my radio button is:
<asp:radiobutton Runat=server onclick='<%# "alert(\"./" + this.PageUrl
+ "\")" %>'></asp:radiobutton>

thanks
tim

Nov 19 '05 #4
by the way, the page language is C#
tim

Nov 19 '05 #5
Hi Tim,

Thanks for your response.
Yes, after some further research I've noticed that the test code I pasted
in the former message is a particular case. More exactly the
behavior(attribute value be html encoded) occurs on both Web Server
Controls or Html Server Controls, and for HtmlInputButton control the
"onclick" attribute is a particular case which will not be html encoded
when being rendered.

So current the actual cause of the behavior is because when the WebServer
Controls or Html Server Controls render themselves to HtmlTextWriter, it'll
call the AttributeCollection's Render method to render their Attributes
collection. Then,
the in the AttributeCollection's Render method, it'll enumerate all the
items in the collection and Call HttpUtility's

HttpUtility.HtmlAttributeEncode method to encode the attribute value.

That's why the " will be replaced by &quot;

Also, though the value will be html encoded, it won't break the url link's
behavior at clientside, we can still use it to redirect.
So what's the detail problem you met when using such encoded url? I think
we may need to find some other means to workaround it.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 19 '05 #6
hi Steven,
i noticed that the javascript still worked, i just didn't like the idea
that my string was being html-encoded without me asking for it! many
thanks for the explanation. it is understandable too because if they
didn't do that, there would be lots of malformed aspx pages out there
with quotes and <>'s breaking tags and attributes.

i'm happy to leave it as is. if it works, don't fix it!!
i appreciate your detective work.
tim

Nov 19 '05 #7
i think the same thing happened. i guess the HtmlAttributeEncode method
grabs the whole attribute, not just the part in the code block, and
formats it as a whole attribute unit.
a good suggestion though, thanks for the input.
tim

Nov 19 '05 #8
Thanks your understanding Tim,

Good Luck! :-)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #9

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

Similar topics

28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
17
by: Joe Laughlin | last post by:
I've not used C much before, so I don't know how robust or good this code is. I'd appreciate any feedback or criticisms anyone has! Thanks, Joe #include <stdio.h> #include <string.h>
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
6
by: Martin Smith | last post by:
How can I stop ASP.NET Encoding things automatically? e.g if I create a button in my code and add the following attribute objNewButton.Attributes.Add("onClick",...
4
by: James Radke | last post by:
Hello, I am attempting to use the proper Try/Catch technique when accessing my Microsoft SQL server database and have a question... If I use something similar to the following: Try set up...
1
by: almarc | last post by:
Problem : Stop an "Unonlaod" when then confirmation is false. Is a really good script but i have just one problem. The problem that i have is when i click on a link. The scipt ask "Do you want to...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
by: mark4asp | last post by:
<form runat="server"automatically adds <divtag to code contained within. Is there a way to stop that? Mixing block-level elements with inline-level elements messes up the HTML becasuse that is...
56
by: valentin tihomirov | last post by:
{ int i = 2; } int i = 1; There is no 'i' defined in the 'parent' context from the moment of declaration on. So what is the problem? They tell us they pursue language simplicity. The rule "do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.