Attributes added at runtime get HTML encoded

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,
 
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!
***************************
 
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 said:
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 said:
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,
 
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)
 
That definitely sounds like what I am looking for, now how do I apply that to
a textbox (for example)?
--
-Steven


Bruce Barker said:
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 said:
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,
 
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==?=" <[email protected]>
| References: <[email protected]>
<#[email protected]>
| Subject: Re: Attributes added at runtime get HTML encoded
| Date: Thu, 14 Jul 2005 13:47:04 -0700
| Lines: 42
| Message-ID: <[email protected]>
| 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)
| >
| >
| > | > > 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
| >
| >
| >
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top