C# 将 Eval 从 ASPX 传递给 Javascript 函数作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/249926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Eval from ASPX to Javascript function as Parameter
提问by naveen
<a id="lblShowTimings"
runat="server"
title='<%# Eval("SHOW_Name") %>'
onclick='PopulateTicketDiv(<%#Eval("SHOW_ID") %>)'> <-- this is the problem
%#Eval("SHOW_Time") %>
</a>
Can Eval be passed as an argument to a javascript function? If so whats the syntax?
可以将 Eval 作为参数传递给 javascript 函数吗?如果是这样,语法是什么?
采纳答案by tvanfosson
Yes. What you want to do is this, though:
是的。不过,您想要做的是:
onclick='<%# "PopulateTicketDiv(" +Eval("SHOW_ID") + " );" %>'
回答by Rob Stevenson-Leggett
Try
尝试
<script type="javascript">
//Pollute the global namespace
var ticketDivID = <%= SHOW_ID %>
</script>
<a id="lblShowTimings" runat="server" title='<%# Eval("SHOW_Name") %>' onclick='PopulateTicketDiv(ticketDivID)'> <%#Eval("SHOW_Time") %></a>
On a side note because you've got runat="server" you can set the onclick from the backend in OnRowDataBound if this is in a grid/repeater or on page_load if not.
附带说明一下,因为你有 runat="server" 你可以在 OnRowDataBound 的后端设置 onclick,如果它在网格/中继器中,或者在 page_load 上,如果不是。
回答by Rohan
The above solution creates problem when you want to pass the string as parameter, you can use following syntax to get through:
当您想将字符串作为参数传递时,上述解决方案会产生问题,您可以使用以下语法来通过:
OnClientClick='<%# String.Format("javascript:return displayDeleteWarning(\"{0}\")", Eval("ItemName").ToString()) %>'
Above line should work irrespective of parameter data type
无论参数数据类型如何,上面的行都应该工作
回答by Sajishanoop
Pls Check this code
请检查此代码
onclick='<%#Eval("DocumentPath","Chk(\"{0}\")") %>'
onclick='<%#Eval("DocumentPath","Chk(\"{0}\")") %>'
回答by Aruna
You can use this syntax within a gridview, repeater or..etc.
您可以在 gridview、repeater 或...等中使用此语法。
<asp:ImageButton
ID="Imagebutton1" runat="server"
ImageUrl="../../common/images/pencil.gif"
OnClientClick='<%# String.Format("EditBankAccount(\"{0}\");", Eval("BankAccountID")) %>'
OnClick="ImgBankAccountsDGEdit_Click"/>
Your JavaScript function would be:
您的 JavaScript 函数将是:
function EditBankAccount(bankaccountid) {
// Your code goes here
// return true OR false based on your requirement
}