Issue with Control.Attributes Property when adding Javascript

Discussion in 'ASP .Net' started by ChrisMiddle10, Oct 12, 2006.

  1. 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!
     
    ChrisMiddle10, Oct 12, 2006
    #1
    1. Advertisements

  2. Are there alternate methods for comparisons?

    For example:
    bool greaterThan( x, y );
    bool lessThanEqualTo( x, y );
     
    ChrisMiddle10, Oct 12, 2006
    #2
    1. Advertisements

  3. ChrisMiddle10

    apathetic Guest

    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
     
    apathetic, Oct 12, 2006
    #3
  4. ChrisMiddle10

    apathetic Guest

    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
     
    apathetic, Oct 12, 2006
    #4
  5. 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?
     
    ChrisMiddle10, Oct 12, 2006
    #5
  6. ChrisMiddle10

    apathetic Guest

    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
     
    apathetic, Oct 12, 2006
    #6
  7. 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 )
    {

    }
     
    ChrisMiddle10, Oct 12, 2006
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.