473,834 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stopping Attributes.Add from converting my JavaScript event code to HTML

I add several JavaScript events (onchange, onkeypress, etc.) to Controls
using the Add method of the Attributes collection. However, if the
JavaScript code contains certain characters, such as & or < or several
others, it converts them to html, such as &amp; or &lt; which can sometimes
cause my scripts not to work. How can I prevent ASP.NET from doing this?
Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/
Jul 24 '06 #1
11 2794
Hi Nathan,

For that is the HTML en/decode classes.

http://msdn.microsoft.com/library/de...codeTopic1.asp

I hope this helps,

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW******** *****@TK2MSFTNG P03.phx.gbl...
>I add several JavaScript events (onchange, onkeypress, etc.) to Controls
using the Add method of the Attributes collection. However, if the
JavaScript code contains certain characters, such as & or < or several
others, it converts them to html, such as &amp; or &lt; which can sometimes
cause my scripts not to work. How can I prevent ASP.NET from doing this?
Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

Jul 25 '06 #2
That would be great if I were using something like the
System.Web.UI.H tmlTextWriter, but I am simply adding attributes objects
created declaratively in my *.aspx file. For example, I have the following
as part of my *.aspx file:
<asp:TextBox ID="txtRed" runat="server" MaxLength="3"
Width="35px">0</asp:TextBox>
And I have the following in my *.aspx.vb file:
txtRed.Attribut es.Add("onchang e", "if(this.value> =0&&this.value< =255){" &
redslider.Clien tSetFunctionNam e & "(this.value);} else{this.value =" &
redslider.Clien tGetFunctionNam e & "();}")
When my page is rendered, this comes out as the following (notice that the
&'s and < are replaced with their HTML representations ):
onchange="if(th is.value>=0&amp ;&amp;this.valu e&lt;=255){slid erRed_setvalue( this.value);}el se{this.value=s liderRed_getval ue();}"
The Server.HtmlDeco de() method would not help me here because the
Attributes.Add( ) method takes a String as it's second parameter, and
Server.HtmlDeco de() would not have any effect on it in my case, so I would
actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG******** ******@TK2MSFTN GP04.phx.gbl...
Hi Nathan,

For that is the HTML en/decode classes.

http://msdn.microsoft.com/library/de...codeTopic1.asp

I hope this helps,

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW******** *****@TK2MSFTNG P03.phx.gbl...
>>I add several JavaScript events (onchange, onkeypress, etc.) to Controls
using the Add method of the Attributes collection. However, if the
JavaScript code contains certain characters, such as & or < or several
others, it converts them to html, such as &amp; or &lt; which can
sometimes cause my scripts not to work. How can I prevent ASP.NET from
doing this? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/


Jul 25 '06 #3
Nathan,

Are you sure you do not need this one. (In this page is a nice sample)
http://msdn2.microsoft.com/en-us/lib...ttributes.aspx

Now you are using this one to add an attribute let say to an listbox, which
is in my idea sure not a piece of javascript
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

I hope this helps,

Cor
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:ew******** *****@TK2MSFTNG P04.phx.gbl...
That would be great if I were using something like the
System.Web.UI.H tmlTextWriter, but I am simply adding attributes objects
created declaratively in my *.aspx file. For example, I have the following
as part of my *.aspx file:
<asp:TextBox ID="txtRed" runat="server" MaxLength="3"
Width="35px">0</asp:TextBox>
And I have the following in my *.aspx.vb file:
txtRed.Attribut es.Add("onchang e", "if(this.value> =0&&this.value< =255){" &
redslider.Clien tSetFunctionNam e & "(this.value);} else{this.value =" &
redslider.Clien tGetFunctionNam e & "();}")
When my page is rendered, this comes out as the following (notice that the
&'s and < are replaced with their HTML representations ):
onchange="if(th is.value>=0&amp ;&amp;this.valu e&lt;=255){slid erRed_setvalue( this.value);}el se{this.value=s liderRed_getval ue();}"
The Server.HtmlDeco de() method would not help me here because the
Attributes.Add( ) method takes a String as it's second parameter, and
Server.HtmlDeco de() would not have any effect on it in my case, so I would
actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG******** ******@TK2MSFTN GP04.phx.gbl...
>Hi Nathan,

For that is the HTML en/decode classes.

http://msdn.microsoft.com/library/de...codeTopic1.asp

I hope this helps,

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW******* ******@TK2MSFTN GP03.phx.gbl...
>>>I add several JavaScript events (onchange, onkeypress, etc.) to Controls
using the Add method of the Attributes collection. However, if the
JavaScript code contains certain characters, such as & or < or several
others, it converts them to html, such as &amp; or &lt; which can
sometimes cause my scripts not to work. How can I prevent ASP.NET from
doing this? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/



Jul 25 '06 #4
I almost always create a .js file and include a reference in the head
tag to it on every page in a web app, if you did it this way you would
not need to add javascript to .net control attributes and could simply
add a function name rather than the full javascript?
Cor Ligthert [MVP] wrote:
Nathan,

Are you sure you do not need this one. (In this page is a nice sample)
http://msdn2.microsoft.com/en-us/lib...ttributes.aspx

Now you are using this one to add an attribute let say to an listbox, which
is in my idea sure not a piece of javascript
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

I hope this helps,

Cor
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:ew******** *****@TK2MSFTNG P04.phx.gbl...
That would be great if I were using something like the
System.Web.UI.H tmlTextWriter, but I am simply adding attributes objects
created declaratively in my *.aspx file. For example, I have the following
as part of my *.aspx file:
<asp:TextBox ID="txtRed" runat="server" MaxLength="3"
Width="35px">0</asp:TextBox>
And I have the following in my *.aspx.vb file:
txtRed.Attribut es.Add("onchang e", "if(this.value> =0&&this.value< =255){" &
redslider.Clien tSetFunctionNam e & "(this.value);} else{this.value =" &
redslider.Clien tGetFunctionNam e & "();}")
When my page is rendered, this comes out as the following (notice that the
&'s and < are replaced with their HTML representations ):
onchange="if(th is.value>=0&amp ;&amp;this.valu e&lt;=255){slid erRed_setvalue( this.value);}el se{this.value=s liderRed_getval ue();}"
The Server.HtmlDeco de() method would not help me here because the
Attributes.Add( ) method takes a String as it's second parameter, and
Server.HtmlDeco de() would not have any effect on it in my case, so I would
actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG******** ******@TK2MSFTN GP04.phx.gbl...
Hi Nathan,

For that is the HTML en/decode classes.

http://msdn.microsoft.com/library/de...codeTopic1.asp

I hope this helps,

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW******** *****@TK2MSFTNG P03.phx.gbl...
I add several JavaScript events (onchange, onkeypress, etc.) to Controls
using the Add method of the Attributes collection. However, if the
JavaScript code contains certain characters, such as & or < or several
others, it converts them to html, such as &amp; or &lt; which can
sometimes cause my scripts not to work. How can I prevent ASP.NET from
doing this? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/
Jul 25 '06 #5
In the first link you provided, the following example is given:

TextBox1.Attrib utes("onblur") = "javascript:ale rt('Hello! Focus lost from
text box!!');"

First of all, the value in this example does not have any characters that
would be affected by Server.HtmlEnco de(). Second, it does not use the Add()
method (I would be willing to use either one, I do prefer the Add() method).
In your second link, the following example is given:

FirstSelect.Att ributes.Add("Mu ltiple", "True")

This example, like you said, is not even JavaScript, and also does not have
any characters that would be affected by Server.HtmlEnco de().
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
Nathan,

Are you sure you do not need this one. (In this page is a nice sample)
http://msdn2.microsoft.com/en-us/lib...ttributes.aspx

Now you are using this one to add an attribute let say to an listbox,
which is in my idea sure not a piece of javascript
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

I hope this helps,

Cor
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:ew******** *****@TK2MSFTNG P04.phx.gbl...
>That would be great if I were using something like the
System.Web.UI. HtmlTextWriter, but I am simply adding attributes objects
created declaratively in my *.aspx file. For example, I have the
following as part of my *.aspx file:
<asp:TextBox ID="txtRed" runat="server" MaxLength="3"
Width="35px">0 </asp:TextBox>
And I have the following in my *.aspx.vb file:
txtRed.Attribu tes.Add("onchan ge", "if(this.value> =0&&this.value< =255){" &
redslider.Clie ntSetFunctionNa me & "(this.value);} else{this.value =" &
redslider.Clie ntGetFunctionNa me & "();}")
When my page is rendered, this comes out as the following (notice that
the &'s and < are replaced with their HTML representations ):
onchange="if(t his.value>=0&am p;&amp;this.val ue&lt;=255){sli derRed_setvalue (this.value);}e lse{this.value= sliderRed_getva lue();}"
The Server.HtmlDeco de() method would not help me here because the
Attributes.Add () method takes a String as it's second parameter, and
Server.HtmlDec ode() would not have any effect on it in my case, so I
would actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG******* *******@TK2MSFT NGP04.phx.gbl.. .
>>Hi Nathan,

For that is the HTML en/decode classes.

http://msdn.microsoft.com/library/de...codeTopic1.asp

I hope this helps,

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW****** *******@TK2MSFT NGP03.phx.gbl.. .
I add several JavaScript events (onchange, onkeypress, etc.) to Controls
using the Add method of the Attributes collection. However, if the
JavaScrip t code contains certain characters, such as & or < or several
others, it converts them to html, such as &amp; or &lt; which can
sometimes cause my scripts not to work. How can I prevent ASP.NET from
doing this? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/



Jul 25 '06 #6
That might be true if you're talking about generating a *.js file using
ASP.NET. The reason I am adding the attributes using ASP.NET code is because
a large amount of the JavaScript is generated (it is different for different
instances of the control). You are correct in that I could create functions
and then call the functions, but since a large amount of my JavaScript is
generated, this would involve creating a lot of functions, many of which
might be only one line, almost doubling the amount of JavaScript, and it
would also involve generating unique names for all the functions. This could
end up being very inefficient, as well as confusing and increasing the
download size of the page.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"tfsmag" <tf****@gmail.c omwrote in message
news:11******** *************@7 5g2000cwc.googl egroups.com...
>I almost always create a .js file and include a reference in the head
tag to it on every page in a web app, if you did it this way you would
not need to add javascript to .net control attributes and could simply
add a function name rather than the full javascript?
Cor Ligthert [MVP] wrote:
>Nathan,

Are you sure you do not need this one. (In this page is a nice sample)
http://msdn2.microsoft.com/en-us/lib...ttributes.aspx

Now you are using this one to add an attribute let say to an listbox,
which
is in my idea sure not a piece of javascript
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

I hope this helps,

Cor
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:ew******* ******@TK2MSFTN GP04.phx.gbl...
That would be great if I were using something like the
System.Web.UI.H tmlTextWriter, but I am simply adding attributes objects
created declaratively in my *.aspx file. For example, I have the
following
as part of my *.aspx file:
<asp:TextBox ID="txtRed" runat="server" MaxLength="3"
Width="35px">0</asp:TextBox>
And I have the following in my *.aspx.vb file:
txtRed.Attribut es.Add("onchang e", "if(this.value> =0&&this.value< =255){"
&
redslider.Clien tSetFunctionNam e & "(this.value);} else{this.value =" &
redslider.Clien tGetFunctionNam e & "();}")
When my page is rendered, this comes out as the following (notice that
the
&'s and < are replaced with their HTML representations ):
onchange="if(th is.value>=0&amp ;&amp;this.valu e&lt;=255){slid erRed_setvalue( this.value);}el se{this.value=s liderRed_getval ue();}"
The Server.HtmlDeco de() method would not help me here because the
Attributes.Add( ) method takes a String as it's second parameter, and
Server.HtmlDeco de() would not have any effect on it in my case, so I
would
actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG******** ******@TK2MSFTN GP04.phx.gbl...
Hi Nathan,

For that is the HTML en/decode classes.

http://msdn.microsoft.com/library/de...codeTopic1.asp

I hope this helps,

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW******* ******@TK2MSFTN GP03.phx.gbl...
I add several JavaScript events (onchange, onkeypress, etc.) to
Controls
using the Add method of the Attributes collection. However, if the
JavaScript code contains certain characters, such as & or < or several
others, it converts them to html, such as &amp; or &lt; which can
sometimes cause my scripts not to work. How can I prevent ASP.NET from
doing this? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/



Jul 25 '06 #7
Nathan,

You cannot use a ship to fly, in my opinion are you trying that by using the
wrong commands, if you want to fly with a ship, than you have a lot of
troubles, with a plane it is much easier.

What your cargo is, is not the first question.

Just my thought, reading your reply.

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
In the first link you provided, the following example is given:

TextBox1.Attrib utes("onblur") = "javascript:ale rt('Hello! Focus lost from
text box!!');"

First of all, the value in this example does not have any characters that
would be affected by Server.HtmlEnco de(). Second, it does not use the
Add() method (I would be willing to use either one, I do prefer the Add()
method). In your second link, the following example is given:

FirstSelect.Att ributes.Add("Mu ltiple", "True")

This example, like you said, is not even JavaScript, and also does not
have any characters that would be affected by Server.HtmlEnco de().
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
>Nathan,

Are you sure you do not need this one. (In this page is a nice sample)
http://msdn2.microsoft.com/en-us/lib...ttributes.aspx

Now you are using this one to add an attribute let say to an listbox,
which is in my idea sure not a piece of javascript
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

I hope this helps,

Cor
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:ew******* ******@TK2MSFTN GP04.phx.gbl...
>>That would be great if I were using something like the
System.Web.UI .HtmlTextWriter , but I am simply adding attributes objects
created declaratively in my *.aspx file. For example, I have the
following as part of my *.aspx file:
<asp:TextBo x ID="txtRed" runat="server" MaxLength="3"
Width="35px"> 0</asp:TextBox>
And I have the following in my *.aspx.vb file:
txtRed.Attrib utes.Add("oncha nge", "if(this.value> =0&&this.value< =255){"
& redslider.Clien tSetFunctionNam e & "(this.value);} else{this.value =" &
redslider.Cli entGetFunctionN ame & "();}")
When my page is rendered, this comes out as the following (notice that
the &'s and < are replaced with their HTML representations ):
onchange="if( this.value>=0&a mp;&amp;this.va lue&lt;=255){sl iderRed_setvalu e(this.value);} else{this.value =sliderRed_getv alue();}"
The Server.HtmlDeco de() method would not help me here because the
Attributes.Ad d() method takes a String as it's second parameter, and
Server.HtmlDe code() would not have any effect on it in my case, so I
would actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG****** ********@TK2MSF TNGP04.phx.gbl. ..
Hi Nathan,

For that is the HTML en/decode classes.

http://msdn.microsoft.com/library/de...codeTopic1.asp

I hope this helps,

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW***** ********@TK2MSF TNGP03.phx.gbl. ..
>I add several JavaScript events (onchange, onkeypress, etc.) to
>Controls using the Add method of the Attributes collection. However, if
>the JavaScript code contains certain characters, such as & or < or
>several others, it converts them to html, such as &amp; or &lt; which
>can sometimes cause my scripts not to work. How can I prevent ASP.NET
>from doing this? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/
>




Jul 26 '06 #8
Nathan, in addition to Cor's suggestions, let me add that there is not much
you can do at this point that can change what is output by the
htmltextwriter. It's this writer which actually minimally HTML-encodes
everything. This is possibly for xhtml comformance, since an & is not a
legal character within an xml document and needs to be escaped. So are
characters like <>. I cant imagine you working around this, without doing
something a bit trival like through the HttpResponse.Fi lter which gives you
a chance to rewrite what is output by the htmltextwriter before it is
transmitted to the page =P
http://msdn2.microsoft.com/en-us/lib...se.filter.aspx

There is full working example in the above url, that shows you howto attach
a filter to the response stream that makes the text displayed to the page
all uppercase.You will be searching for &amp; or whatever other html code
characters you dont want, and replacing them with the original you had
wanted.

That said, can you explain further why you want to work around this ? I dont
understand why your script shouldnt work *sometimes* with those characters
escaped.

Your problem, in my humble opinion is somewhere else, possibly in the js
code that you are writing, but i cant say much since the code you pasted
works very well *always* for me. I have pasted the code i tested below. Let
us know, what you are doing differently and under what circumstance its not
working for you.

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<script runat="server">
protected void Page_Load(objec t sender, EventArgs e)
{
TextBox1.Attrib utes.Add("oncha nge", "if (parseInt(this. value) >= 0
&& parseInt(this.v alue) <= 255){ alert('more than 0 and less than a
100');}");
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Text="" runat="server"> </asp:TextBox>
</div>
</form>
</body>
</html>

"Cor Ligthert [MVP]" <no************ @planet.nlha scritto nel messaggio
news:OU******** ******@TK2MSFTN GP03.phx.gbl...
Nathan,

You cannot use a ship to fly, in my opinion are you trying that by using
the wrong commands, if you want to fly with a ship, than you have a lot of
troubles, with a plane it is much easier.

What your cargo is, is not the first question.

Just my thought, reading your reply.

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>In the first link you provided, the following example is given:

TextBox1.Attri butes("onblur") = "javascript:ale rt('Hello! Focus lost from
text box!!');"

First of all, the value in this example does not have any characters that
would be affected by Server.HtmlEnco de(). Second, it does not use the
Add() method (I would be willing to use either one, I do prefer the Add()
method). In your second link, the following example is given:

FirstSelect.At tributes.Add("M ultiple", "True")

This example, like you said, is not even JavaScript, and also does not
have any characters that would be affected by Server.HtmlEnco de().
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:%2******* ********@TK2MSF TNGP05.phx.gbl. ..
>>Nathan,

Are you sure you do not need this one. (In this page is a nice sample)
http://msdn2.microsoft.com/en-us/lib...ttributes.aspx

Now you are using this one to add an attribute let say to an listbox,
which is in my idea sure not a piece of javascript
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

I hope this helps,

Cor
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:ew****** *******@TK2MSFT NGP04.phx.gbl.. .
That would be great if I were using something like the
System.Web.U I.HtmlTextWrite r, but I am simply adding attributes objects
created declaratively in my *.aspx file. For example, I have the
following as part of my *.aspx file:
<asp:TextB ox ID="txtRed" runat="server" MaxLength="3"
Width="35px" >0</asp:TextBox>
And I have the following in my *.aspx.vb file:
txtRed.Attri butes.Add("onch ange", "if(this.value> =0&&this.value< =255){"
& redslider.Clien tSetFunctionNam e & "(this.value);} else{this.value =" &
redslider.Cl ientGetFunction Name & "();}")
When my page is rendered, this comes out as the following (notice that
the &'s and < are replaced with their HTML representations ):
onchange="if (this.value>=0& amp;&amp;this.v alue&lt;=255){s liderRed_setval ue(this.value); }else{this.valu e=sliderRed_get value();}"
The Server.HtmlDeco de() method would not help me here because the
Attributes.A dd() method takes a String as it's second parameter, and
Server.HtmlD ecode() would not have any effect on it in my case, so I
would actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG***** *********@TK2MS FTNGP04.phx.gbl ...
Hi Nathan,
>
For that is the HTML en/decode classes.
>
http://msdn.microsoft.com/library/de...codeTopic1.asp
>
I hope this helps,
>
Cor
>
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:OW**** *********@TK2MS FTNGP03.phx.gbl ...
>>I add several JavaScript events (onchange, onkeypress, etc.) to
>>Control s using the Add method of the Attributes collection. However,
>>if the JavaScript code contains certain characters, such as & or < or
>>several others, it converts them to html, such as &amp; or &lt; which
>>can sometimes cause my scripts not to work. How can I prevent ASP.NET
>>from doing this? Thanks.
>--
>Nathan Sokalski
>nj********@hotm ail.com
>http://www.nathansokalski.com/
>>
>
>




Jul 26 '06 #9
I haven't really had any problems with the JavaScript not working because of
this, but it worries (not to mention frustrates) me that I might in the
future for the following reason:

Cross-browser compatibility: I am afraid that some new version of some
browser (or maybe even an existing one, as of now I have only tested on IE6)
will not like this incorrect JavaScript syntax
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Alessandro Zifiglio" <AlessandroZifi glio @ -h-o-t-m-a-i-l-c-o-mwrote in
message news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
Nathan, in addition to Cor's suggestions, let me add that there is not
much you can do at this point that can change what is output by the
htmltextwriter. It's this writer which actually minimally HTML-encodes
everything. This is possibly for xhtml comformance, since an & is not a
legal character within an xml document and needs to be escaped. So are
characters like <>. I cant imagine you working around this, without doing
something a bit trival like through the HttpResponse.Fi lter which gives
you a chance to rewrite what is output by the htmltextwriter before it is
transmitted to the page =P
http://msdn2.microsoft.com/en-us/lib...se.filter.aspx

There is full working example in the above url, that shows you howto
attach a filter to the response stream that makes the text displayed to
the page all uppercase.You will be searching for &amp; or whatever other
html code characters you dont want, and replacing them with the original
you had wanted.

That said, can you explain further why you want to work around this ? I
dont understand why your script shouldnt work *sometimes* with those
characters escaped.

Your problem, in my humble opinion is somewhere else, possibly in the js
code that you are writing, but i cant say much since the code you pasted
works very well *always* for me. I have pasted the code i tested below.
Let us know, what you are doing differently and under what circumstance
its not working for you.

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<script runat="server">
protected void Page_Load(objec t sender, EventArgs e)
{
TextBox1.Attrib utes.Add("oncha nge", "if (parseInt(this. value) >= 0
&& parseInt(this.v alue) <= 255){ alert('more than 0 and less than a
100');}");
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Text="" runat="server"> </asp:TextBox>
</div>
</form>
</body>
</html>

"Cor Ligthert [MVP]" <no************ @planet.nlha scritto nel messaggio
news:OU******** ******@TK2MSFTN GP03.phx.gbl...
>Nathan,

You cannot use a ship to fly, in my opinion are you trying that by using
the wrong commands, if you want to fly with a ship, than you have a lot
of troubles, with a plane it is much easier.

What your cargo is, is not the first question.

Just my thought, reading your reply.

Cor

"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:%2******* *********@TK2MS FTNGP06.phx.gbl ...
>>In the first link you provided, the following example is given:

TextBox1.Attr ibutes("onblur" ) = "javascript:ale rt('Hello! Focus lost
from text box!!');"

First of all, the value in this example does not have any characters
that would be affected by Server.HtmlEnco de(). Second, it does not use
the Add() method (I would be willing to use either one, I do prefer the
Add() method). In your second link, the following example is given:

FirstSelect.A ttributes.Add(" Multiple", "True")

This example, like you said, is not even JavaScript, and also does not
have any characters that would be affected by Server.HtmlEnco de().
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:%2****** *********@TK2MS FTNGP05.phx.gbl ...
Nathan,

Are you sure you do not need this one. (In this page is a nice sample)
http://msdn2.microsoft.com/en-us/lib...ttributes.aspx

Now you are using this one to add an attribute let say to an listbox,
which is in my idea sure not a piece of javascript
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

I hope this helps,

Cor
"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
news:ew***** ********@TK2MSF TNGP04.phx.gbl. ..
That would be great if I were using something like the
System.Web. UI.HtmlTextWrit er, but I am simply adding attributes
objects created declaratively in my *.aspx file. For example, I have
the following as part of my *.aspx file:
>
>
<asp:TextBo x ID="txtRed" runat="server" MaxLength="3"
Width="35px ">0</asp:TextBox>
>
>
And I have the following in my *.aspx.vb file:
>
>
txtRed.Attr ibutes.Add("onc hange",
"if(this.va lue>=0&&this.va lue<=255){" &
redslider.C lientSetFunctio nName & "(this.value);} else{this.value =" &
redslider.C lientGetFunctio nName & "();}")
>
>
When my page is rendered, this comes out as the following (notice that
the &'s and < are replaced with their HTML representations ):
>
>
onchange="i f(this.value>=0 &amp;&amp;this. value&lt;=255){ sliderRed_setva lue(this.value) ;}else{this.val ue=sliderRed_ge tvalue();}"
>
>
The Server.HtmlDeco de() method would not help me here because the
Attributes. Add() method takes a String as it's second parameter, and
Server.Html Decode() would not have any effect on it in my case, so I
would actually be passing the same String. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/
>
"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:OG**** **********@TK2M SFTNGP04.phx.gb l...
>Hi Nathan,
>>
>For that is the HTML en/decode classes.
>>
>http://msdn.microsoft.com/library/de...codeTopic1.asp
>>
>I hope this helps,
>>
>Cor
>>
>"Nathan Sokalski" <nj********@hot mail.comschreef in bericht
>news:OW*** **********@TK2M SFTNGP03.phx.gb l...
>>>I add several JavaScript events (onchange, onkeypress, etc.) to
>>>Contro ls using the Add method of the Attributes collection. However,
>>>if the JavaScript code contains certain characters, such as & or < or
>>>severa l others, it converts them to html, such as &amp; or &lt; which
>>>can sometimes cause my scripts not to work. How can I prevent ASP.NET
>>>from doing this? Thanks.
>>--
>>Nathan Sokalski
>>nj********@hotm ail.com
>>http://www.nathansokalski.com/
>>>
>>
>>
>
>




Jul 26 '06 #10

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

Similar topics

6
5920
by: Dave | last post by:
I have a situation where I want to react to a ctrl-click on a <span> and it works in Netscape and Firefox browsers but in IE I have a problem. In IE I do catch the ctrl-click but IE also renders the span in inverse video, essentially selecting the item. Here is a short sample that demonstrates the issue: <html><head> <script type="text/javascript">
3
9930
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ========================= <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"
5
1996
by: Rob Lynch | last post by:
This one has me perplexed from vs.net web app add a new aspx pag add 3 textbox's and a command butto in the command buttons click event add response.write "button1 click from any textbox press the enter ke the Button1 Click event fire
1
3908
by: Bruce | last post by:
I use btnSave.Attributes.Add("onclick", "ShowMessage()") to link my web control button to a JavaScript function. It works well until I added a Validation control into the page. After that, even the all of the validations passed, my ShowMessage() still does not get triggered. When checking the view source carefully, I found that the btnSave button have 2 onClick events defined in the tag.
8
2820
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);" 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
3
1940
by: Jon Delano | last post by:
Hi I was wondering if it were possible to somehow stop a page from posting back to the server and running the server side code. I have a datagrid and the first column is basically a button. I have added a javascript function to the button : Dim btnView As LinkButton = e.Item.Cells(0).Controls(0) btnView.Attributes.Add("onclick", "CheckDictationMode();")
11
1422
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or < or several others, it converts them to html, such as &amp; or &lt; which can sometimes cause my scripts not to work. How can I prevent ASP.NET from doing this? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
6
2668
by: Andrew Poulos | last post by:
I'm using Windows Media Audio, via a WAX file, and I have some custom controls to start and stop the audio. While it works in IE, FF gives an error. The HTML looks like this: <object id='obj' type='audio/x-ms-wax' style='position:absolute; left:0px; top:0px;' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95' codeBase='http://activex... blah width='100' height='100'> <param name='FileName' value='wma_sample.wax'>
2
2112
by: Safalra | last post by:
Recently I've rewritten much of my old Javascript code to use DOM functions so that enhancements can be attached to documents without needing to alter the HTML code. I assumed that adding event handlers to document.body would have the same effect as adding those event handlers through attributes of the body element, but this turned out not to be the case for key events. Take this example page (IE-specific code removed): ...
0
10800
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10516
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9339
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7762
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5800
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3987
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.