Okay, i think i've tried 3-4 methods here from stackoverflow, but none seems to work.
I've got:
OnClientClick='<%# Eval("albumName", "doConfirm(\"delete\", \"{0}\");").ToString() %>'
but in html it renders as:
onclick="doConfirm("delete", "Test");"
Also tried making a method to call:
public string CreateConfirmation(String action, String item) {
return String.Format(@"return confirm('Sikker på du vil {0}: {1}');", action, item);
}
With this:
OnClientClick='<%# CreateConfirmation("delete", (string)Eval(albumName)) %>'
But gives me exact same problem.... So im pretty lost?
Even though this question is 5 years old, I wanted to follow up as I had the same issue with an ImageButton and was able to resolve it using a HiddenField.
Background: I have a Web User Control which I wanted to have a help button displayed if there was help available.
I added a HiddenField and an ImageButton to the User Control. I then created a property on the control so the developer may add help text.
ASPX Page
Code Behind (CS File)
This gets around the issue as the text belongs to the hidden field instead of trying to include it within the JavaScript for the OnClientClick property.
BTW: I cannot copy and paste so this code may contain some typos but I believe it is correct. At least it points the way so you may be able to work around the issue.
I apologize in advance for such a long answer, but I wanted to be thorough.
This is apparently a "security" feature in .Net 4.0 (and higher). You can read more about it at:
All of the above links also recommend declaring a public class to override the behavior:
and then adding this to the
<system.web>
element in your web.config:This definitely fixes the rendering problem, so that quotes and apostrophes render as
"
and'
(as expected).That said, I tested your problem with the following:
and in the code-behind:
I was able to duplicate your rendering problem:
When any of the buttons were clicked, the JavaScript function ignored the HTML encoding, alerting me to:
so while it looks funky in the source, having quotes and apostrophes render as
"
and'
doesn't really appear to affect anything.Try the following:
JS:
Just attach the event server side inside
rowDataBound
event like, (you can replace linkbutton with Button)