473,326 Members | 2,126 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,326 software developers and data experts.

Attributes added at runtime get HTML encoded

Hi there,

I am having a problem where if i add an attribute to a control at runtime
the rendered attribute is HTML encoded.

For example, on a textbox:

textBox.Attributes["onKeyDown"] = "if(x && y) alert('hello');";

Gets rendered as:

onKeyDown="if(x && y) alert('hello');"

Does anyone know how to avoid this behavior?

Thanks,

--
-Steven
Nov 19 '05 #1
5 2785
In textBox on ASPX page (not code behind), add:

onkeydown=javascript:CheckKey()

Then, create a JavaScript routine to handle the keydown event:

function CheckKey()
{
if('x') alert('hello');
}
Note: I made it so entering x fires an alert, as I am not sure what your x
and y do.

Second note: This will not allow you to stop entry of certain values into a
textbox. That requires much more JavaScript than a simple event handler like
this one.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"Steven Berkovitz" wrote:
Hi there,

I am having a problem where if i add an attribute to a control at runtime
the rendered attribute is HTML encoded.

For example, on a textbox:

textBox.Attributes["onKeyDown"] = "if(x && y) alert('hello');";

Gets rendered as:

onKeyDown="if(x && y) alert('hello');"

Does anyone know how to avoid this behavior?

Thanks,

--
-Steven

Nov 19 '05 #2
Is there no way to prevent ASP.NET from HtmlEncoding the attribute?

This is a good workaround but requires unnecessary additional code for
simple things like alerts.

--
-Steven
"Cowboy (Gregory A. Beamer) - MVP" wrote:
In textBox on ASPX page (not code behind), add:

onkeydown=javascript:CheckKey()

Then, create a JavaScript routine to handle the keydown event:

function CheckKey()
{
if('x') alert('hello');
}
Note: I made it so entering x fires an alert, as I am not sure what your x
and y do.

Second note: This will not allow you to stop entry of certain values into a
textbox. That requires much more JavaScript than a simple event handler like
this one.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"Steven Berkovitz" wrote:
Hi there,

I am having a problem where if i add an attribute to a control at runtime
the rendered attribute is HTML encoded.

For example, on a textbox:

textBox.Attributes["onKeyDown"] = "if(x && y) alert('hello');";

Gets rendered as:

onKeyDown="if(x && y) alert('hello');"

Does anyone know how to avoid this behavior?

Thanks,

--
-Steven

Nov 19 '05 #3
the HtmlWriter, has encoding conventions for each attribute (default is
encode). onkeydown is not included in its default set, so it defualts to
encode. use the RegisterAttribute() method to setup the proper encoding for
this attribute.

-- bruce (sqlwork.com)
"Steven Berkovitz" <mb****@community.nospam> wrote in message
news:60**********************************@microsof t.com...
Hi there,

I am having a problem where if i add an attribute to a control at runtime
the rendered attribute is HTML encoded.

For example, on a textbox:

textBox.Attributes["onKeyDown"] = "if(x && y) alert('hello');";

Gets rendered as:

onKeyDown="if(x && y) alert('hello');"

Does anyone know how to avoid this behavior?

Thanks,

--
-Steven

Nov 19 '05 #4
That definitely sounds like what I am looking for, now how do I apply that to
a textbox (for example)?
--
-Steven
"Bruce Barker" wrote:
the HtmlWriter, has encoding conventions for each attribute (default is
encode). onkeydown is not included in its default set, so it defualts to
encode. use the RegisterAttribute() method to setup the proper encoding for
this attribute.

-- bruce (sqlwork.com)
"Steven Berkovitz" <mb****@community.nospam> wrote in message
news:60**********************************@microsof t.com...
Hi there,

I am having a problem where if i add an attribute to a control at runtime
the rendered attribute is HTML encoded.

For example, on a textbox:

textBox.Attributes["onKeyDown"] = "if(x && y) alert('hello');";

Gets rendered as:

onKeyDown="if(x && y) alert('hello');"

Does anyone know how to avoid this behavior?

Thanks,

--
-Steven


Nov 19 '05 #5
Hi Steven,

As for the WebControl's Attributes being HtmlEncoded when rendering, it is
a internal behavior of the HtmlTextWriter as Bruce has mentioned. And yes,
the HtmlTextWriter did has a RegisterAttribute Method which is like:

static void RegisterAttribute(string name, HtmlTextWriterAttribute key,
bool fEncode)

the fEncode just used to specify whether the attribute value will be
htmlencoded. However, the problem is that this method is private so that we
can't call it outside the class itself. So currently we're limited to the
internal Attributes collection and the HtmlTextWriter's default setting.
I'll suggest you use the workaround Gregory mentioend if you do feel it
necessary to avoid the script code being encoded.

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.)

--------------------
| Thread-Topic: Attributes added at runtime get HTML encoded
| thread-index: AcWItTEtpBR/JbBYTYqCoYuABCyaug==
| X-WBNR-Posting-Host: 70.26.197.142
| From: "=?Utf-8?B?U3RldmVuIEJlcmtvdml0eg==?=" <mb****@community.nospam>
| References: <60**********************************@microsoft.co m>
<#G**************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: Attributes added at runtime get HTML encoded
| Date: Thu, 14 Jul 2005 13:47:04 -0700
| Lines: 42
| Message-ID: <32**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:112301
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| That definitely sounds like what I am looking for, now how do I apply
that to
| a textbox (for example)?
| --
| -Steven
|
|
| "Bruce Barker" wrote:
|
| > the HtmlWriter, has encoding conventions for each attribute (default is
| > encode). onkeydown is not included in its default set, so it defualts
to
| > encode. use the RegisterAttribute() method to setup the proper encoding
for
| > this attribute.
| >
| > -- bruce (sqlwork.com)
| >
| >
| > "Steven Berkovitz" <mb****@community.nospam> wrote in message
| > news:60**********************************@microsof t.com...
| > > Hi there,
| > >
| > > I am having a problem where if i add an attribute to a control at
runtime
| > > the rendered attribute is HTML encoded.
| > >
| > > For example, on a textbox:
| > >
| > > textBox.Attributes["onKeyDown"] = "if(x && y) alert('hello');";
| > >
| > > Gets rendered as:
| > >
| > > onKeyDown="if(x && y) alert('hello');"
| > >
| > > Does anyone know how to avoid this behavior?
| > >
| > > Thanks,
| > >
| > >
| > >
| > > --
| > > -Steven
| >
| >
| >
|

Nov 19 '05 #6

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

Similar topics

1
by: Alexander Kervero | last post by:
Hi ,today i was reading diveinto python book,in chapter 5 it has a very generic module to get file information,html,mp3s ,etc. The code of the example is here :...
1
by: Duncan Aitken | last post by:
All, I have a question regarding modifying the metadata of a compiled assembly at runtime. I am creating objects to be added to a PropertyGrid control. In particular, I would like to modify the...
7
by: grwalker | last post by:
I have some classes that have the <Serializable()> attribute applied, which of course by default serializes the class properties as elements. What I would like to do is to be able to override this...
1
by: Randall Parker | last post by:
I'm wondering if there are general rules for the use of HTML attributes with ASP tags? For example, one can set an id for an asp:DataGrid and it will show up as the id for the resulting HTML...
1
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,...
1
by: Gérard Talbot | last post by:
Hello, According to DOM 2 Core, the attribute "nodeRef.attributes" is "A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise."...
40
by: Shmuel (Seymour J.) Metz | last post by:
I'd like to include some Hebrew names in a web page. HTML 4 doesn't appear to include character attributes for ISO-8859-8. I'd prefer avoiding numeric references, e.g.,...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.