Issue with Control.Attributes Property when adding Javascript

  • Thread starter Thread starter ChrisMiddle10
  • Start date Start date
C

ChrisMiddle10

Sorry to bother everyone with this question, but the answer to this the
question is difficult to search for. I want to add javascript to an
HtmlInputButton control attribute. A portion of the javascript goes
something like this:

if( x < y && x != y )
{
// do something
}

It is rendered as follows:

if( x &lt; y &amp;&amp; x != y )
{
// do something
}

How do I preserve the literal "<" and "&"?

Thanks a lot!
 
Are there alternate methods for comparisons?

For example:
bool greaterThan( x, y );
bool lessThanEqualTo( x, y );
 
I want to add javascript to an HtmlInputButton control attribute ...
How do I preserve the literal "<" and "&"?

You don't. When you call Attributes.Add(), ASP.NET automatically
converts them to their HTML entity references, which is correct. Are
you finding that this method doesn't work correctly?

Tim
 
To clarify, the output you are getting is the correct way to output
those characters in an attribute. Why do you need to change this
behaviour? Do you find that the script fails to execute?

Tim
 
Well I suppose the method is working "correctly", but from what I can
tell it is limiting my ability to insert javascript into the attribute
value. I need to do the comparison somehow. I'd prefer to keep the
javascript in that particular button's element.

Can you think of a good work-around? Is the Render method going to be
my best bet for getting the results I want?
 
I want to add javascript to an HtmlInputButton control attribute ...
How do I preserve the literal "<" and "&"?

You don't. When you call Attributes.Add(), ASP.NET automatically
converts them to their HTML entity references, which is correct. Are
you finding that this method doesn't work correctly?

Tim
 
In response to your prior post for clarification:

Yes, the script does not execute due to a syntax error. When I view the
source I see something like:

if( x &lt; y &amp;&amp; y &gt; x )
{

}

The above is definitely not correct syntax. What I need is:

if( x < y && y > x )
{

}
 
Back
Top