Adding a clientside onload function to an ASP.NET control in 4.0 encodes my quotes
I'm trying to add a Javascript function to the onLoad event of a asp:Panel. It goes something like this:
string clickFunction = "$('[id*=lblHiddenPageArray]').text('');"
PagesPanel.Attribues.Add("onLoad", clickFunction);
I'm attaching this function to other controls (Checkboxes and Buttons) and it's working fine. But in the PagesPanel (my asp:Panel control) it HTMLEncodes the function. The output source looks like this:
onLoad="$('[id*=lblHiddenPageArray]').text('');
I've tried to Server.开发者_运维技巧HTMLDecode it on the assignment, but I get the same thing. I've run into this before in .net 4.0. Surely there's a way to escape the characters or something?
I think that's proper behavior. The attribute values are unencoded by the browser before being executed. A simple example:
<!DOCTYPE html>
<html>
<head>
<title>foo</title>
</head>
<body onload="alert('foo');">
<p>foo</p>
</body>
</html>
Just in case you for some reason can't un-HTML-encode things in your head ;-), Firebug in Firefox, the WebKit inspector in Safari and Chrome, and Dragonfly in Opera will do it for you automatically.
精彩评论