admin管理员组文章数量:1398536
What is the proper way to pass an asp value into javascript? For instance, this line of code says I am missing a token..
<asp:ImageButton runat="server" ImageUrl="/ESDNET/Images/Icons/add.png" OnClientClick="return 'ShowNewNoteForm("<%# Eval("SuggestionID").ToString() %>");'/>
What is the proper way to pass an asp value into javascript? For instance, this line of code says I am missing a token..
<asp:ImageButton runat="server" ImageUrl="/ESDNET/Images/Icons/add.png" OnClientClick="return 'ShowNewNoteForm("<%# Eval("SuggestionID").ToString() %>");'/>
Share
Improve this question
asked Sep 13, 2013 at 19:35
MattMatt
5,69014 gold badges50 silver badges59 bronze badges
1
-
1
You are missing a
"
– epascarello Commented Sep 13, 2013 at 19:38
4 Answers
Reset to default 3eval is evil. You don't need to build JavaScript with server side code, it's a good way to cause yourself to have a massive headache.
To pass data to JavaScript, use custom [data-*]
attributes and JSON.
<asp:ImageButton runat="server" ID="Button" />
ascx.cs
Button.Attributes["data-foo"] =
new {
bar = "baz",
fizz = "buzz"
}.ToJSON();
ToJSON
extension method
public static string ToJSON(this object source)
{
var jss = new JavaScriptSerializer();
return jss.Serialize(source);
}
Access in JS via jQuery:
var foo;
foo = $('[data-foo]').data('foo');
console.log(foo.bar); //'baz'
console.log(foo.fizz); //'buzz';
One of the most important things about this setup is that C# will correctly JSON and HTML encode the data (in that order), following that, the JavaScript api will correctly decode the HTML attribute, and jQuery will correctly parse the JSON object.
What this means is that if you add special characters or encoded values to your C# object, you'll end up with those special characters or encoded values in your JavaScript object, without having to mess with anything to get it to work.
Your quotes are wrong. You should open and close the OnClientClick assignment with, double quotes, and anything inside your javascript code should be single quotes. Then, inside your breaking tags, you need to go back to double quotes. This should work...
<asp:ImageButton runat="server" ImageUrl="/ESDNET/Images/Icons/add.png" OnClientClick="return ShowNewNoteForm('<%# Eval("SuggestionID").ToString() %>');"/>
Finally pulled it off with the following:
<asp:ImageButton runat="server" ID="addNote" ImageUrl="/ESDNET/Images/Icons/add.png" OnClientClick='<%# String.Format("return ShowNewNoteForm(\"{0}\")", Eval("SuggestionID")) %>'/>
Try replacing the '\' with an additional '"'
<asp:ImageButton runat="server" ID="addNote" ImageUrl="/ESDNET/Images/Icons/add.png" OnClientClick='<%# String.Format("return ShowNewNoteForm(""{0}"")", Eval("SuggestionID")) %>'/>
本文标签: aspnetHow to Properly pass Eval to javascript functionStack Overflow
版权声明:本文标题:asp.net - How to Properly pass Eval to javascript function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744108465a2591177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论