0

I have an asp.net web page.

Is there a possibility to just get 'real' single quotes client side?

Server side:

this.Attributes["onclick"] = "$('#button').click();";

Client side:

onclick="$('#button').click();" 

gives an error.

Alert works just fine, i.e. Client side:

onclick="alert('hello#amp;39;);".

Seems like I need 'real' single quotes to work with jQuery.

royu
  • 377
  • 2
  • 5
  • 15
  • possible duplicate of [ASP.NET single quotes are converted to '](http://stackoverflow.com/questions/3013912/asp-net-single-quotes-are-converted-to-39) – Jason Apr 10 '12 at 11:29
  • dup of http://stackoverflow.com/questions/3013912/asp-net-single-quotes-are-converted-to-39 – stark Apr 10 '12 at 11:42
  • Jason: Yes, I made those conclusions already. I don't like the idea to use html elements in the source file and use runat=server. It will also become a problem when adding dynamic controls to a page. I know this change has to do with securtiy (XSS), but it seems just like a bug if you ask me. Seems like MS did not thoroughly tested cases like above or they need start loving js some more ;) – royu Apr 10 '12 at 11:42
  • Stark that's the same link as the one Jason posted – royu Apr 10 '12 at 11:51
  • See if [this](http://stackoverflow.com/questions/5749590/passing-strings-with-single-qoute-from-mvc-razor-to-javascript) helps any. – ataddeini Apr 10 '12 at 11:55
  • ataddeini - As far as I know I can't use Html.Raw in standard asp.net and escaping doesn't work, it always comes up with ' (seems like a bug). – royu Apr 10 '12 at 16:16
  • the semicolons and the apostrophes are at war. – defines Jul 09 '13 at 19:31

1 Answers1

1

Is this standard ASP.NET, or ASP.NET MVC? It appears that the output is being escaped, but without sample code or more details I cannot be certain.

Your code example of

this.Attributes["onclick"] = "$('#button').click();";

How is that rendered from the server? From a control?

Updated

This attribute value encoding was introduced in .NET 4.0. If you must you can set the controlRenderingCompatibilityVersion in your web.config to "3.5" see pages Element (ASP.NET Settings Schema) & PagesSection.ControlRenderingCompatibilityVersion Property.

Could you (instead of placing the onclick logic directly in the attribute value) record the function elsewhere and set the function name as the onclick value? Or register the click handler client side using jQuery?

Marc Gagne
  • 819
  • 5
  • 7
  • Standard ASP.NET. Rendered in page code behind (prerender) for a hyperlink (HyperLink control on page). – royu Apr 10 '12 at 11:54
  • Thanks, I've created a sample app and can reproduce your issue, and after a quick google search I see you are not alone, plenty of others are having the same issue after upgrading to .NET 4.0. I've updated my answer with a few suggestions. – Marc Gagne Apr 10 '12 at 12:37
  • Thx Marc. Yes, I made a jQuery ready function to assign the click handler. In this case it will do, I wonder what to do when things are dynamic ... – royu Apr 10 '12 at 15:45
  • You could use the `ClientScriptManager` as shown here: http://msdn.microsoft.com/en-us/library/btf44dc9.aspx – Marc Gagne Apr 10 '12 at 16:52
  • Thanks Marc. I know the ClientScriptBlocks. If you ask me that's kind of huge for something small I am trying to accomplish ;) – royu Apr 10 '12 at 20:47
  • True enough :) p.s if you found my answer useful at all I wouldn't mind an upvote ;) – Marc Gagne Apr 10 '12 at 21:00