Escaping the & in Attributes.Add()

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Oct 2, 2007.

  1. 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 representation of & which causes
    my JavaScript not to work. What can I do to prevent the Add() method from
    modifying my value? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Oct 2, 2007
    #1
    1. Advertising

  2. Hello Nathan,

    > 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
    > representation of & which causes my JavaScript not to work. What
    > can I do to prevent the Add() method from modifying my value? Thanks.


    Any attribute must escape the HTML special characters. An onCLick as well.
    Are you sure this is causing the error?

    --
    Jesse Houwing
    jesse.houwing at sogeti.nl
    Jesse Houwing, Oct 2, 2007
    #2
    1. Advertising

  3. Yes, I am sure because I create the value by assigning a value to a String
    variable, and then using that variable in the Attributes.Add() method. For
    example:

    Dim onmouseover as String= "my javascript code" 'This contains &'s in my
    actual code
    mycontrol.Attributes.Add("onmouseover",onmouseover)

    When I used this technique and ran a Debug session, the String onmouseover
    looked exactly as I wanted, but the rendered code had replaced the &'s with
    & which is what I am trying to avoid. Any ideas? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Jesse Houwing" <> wrote in message
    news:...
    > Hello Nathan,
    >
    >> 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
    >> representation of &amp; which causes my JavaScript not to work. What
    >> can I do to prevent the Add() method from modifying my value? Thanks.

    >
    > Any attribute must escape the HTML special characters. An onCLick as well.
    > Are you sure this is causing the error?
    >
    > --
    > Jesse Houwing
    > jesse.houwing at sogeti.nl
    >
    >
    Nathan Sokalski, Oct 2, 2007
    #3
  4. Hello Nathan,

    > Yes, I am sure because I create the value by assigning a value to a
    > String variable, and then using that variable in the Attributes.Add()
    > method. For example:
    >
    > Dim onmouseover as String= "my javascript code" 'This contains &'s in
    > my
    > actual code
    > mycontrol.Attributes.Add("onmouseover",onmouseover)
    > When I used this technique and ran a Debug session, the String
    > onmouseover looked exactly as I wanted, but the rendered code had
    > replaced the &'s with &amp; which is what I am trying to avoid. Any
    > ideas? Thanks.


    There are no idea's. The spec says that every attribute must be encoded using
    html encoding. That includes the &. The javascript perser should work.

    If you run it in Firefox, what is the message on the javascript console when
    you click the html element in question (firefox is usually a bit mor eoncise
    in it's error messages). There must be somethign else wrong here.

    I tested the code below:

    <html>
    <body>
    <form>
    <input type="button" onclick="alert(true &amp;&amp; true);" value="test">
    </form>
    </body>
    </html>

    And it works in both Internet Explorer and Firefox (it should say true in
    the alert when you click the button).

    Jesse


    > "Jesse Houwing" <> wrote in message
    > news:...
    >
    >> Hello Nathan,
    >>
    >>> 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
    >>> representation of &amp; which causes my JavaScript not to work. What
    >>> can I do to prevent the Add() method from modifying my value?
    >>> Thanks.
    >>>

    >> Any attribute must escape the HTML special characters. An onCLick as
    >> well. Are you sure this is causing the error?
    >>
    >> --
    >> Jesse Houwing
    >> jesse.houwing at sogeti.nl

    --
    Jesse Houwing
    jesse.houwing at sogeti.nl
    Jesse Houwing, Oct 2, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    1
    Views:
    478
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,809
    bruce barker
    Jan 31, 2007
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    370
    Bruno Desthuilliers
    Jun 20, 2007
  4. Nathan Sokalski

    Escaping the & in Attributes.Add()

    Nathan Sokalski, Oct 2, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    490
    Jesse Houwing
    Oct 2, 2007
  5. Daniel Sheppard
    Replies:
    0
    Views:
    154
    Daniel Sheppard
    Oct 28, 2005
Loading...

Share This Page