ASP.NET single quotes are converted to '

Posted by Monty on Stack Overflow See other posts from Stack Overflow or by Monty
Published on 2010-06-10T11:27:17Z Indexed on 2010/06/14 15:12 UTC
Read the original article Hit count: 229

Filed under:
|
|

Note: Most probably this will be a double question, but since I haven't found a clear answer, I'm asking it anyway.

In ASP.NET I'd like to add some JavaScript to the onclick event of a CheckBox. I've simplified the case to this:

<asp:CheckBox ID="TestCheckBox" runat="server" onclick="alert('test');" Text="Test" />

The resulting HTML is as follows:

<input id="MainContainer_TestCheckBox" type="checkbox" name="ctl00$MainContainer$TestCheckBox" onclick="alert(&#39;test&#39;);" /><label for="MainContainer_TestCheckBox">Test</label> 

What particularly bothers me is that a single quote 'automatically' gets converted into '&#39;'. If I omit the onclick in the markup and assign it in Page_Load, the exact same results show in the HTML.

protected void Page_Load(object sender, EventArgs e)
{
    this.TestCheckBox.Attributes["onclick"] = "alert('test');";
}

Anyone got a clue about what's happening? Or how to fix/ avoid it?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET