如何使用 Eval 将 javascript 函数与 OnClientClick 事件绑定?
我的链接按钮 -
<asp:LinkButton runat="server" ID="lbtnEdit" Text="edit" OnClientClick="javascript:msgDisp('<%# Eval(LocationId).toString() %>')" />
和 javascript msgDisp 是
<script type="text/javascript" language="javascript">
function msgDisp(lid) {
alert(lid);
}
</script>
- 但它没有在弹出窗口中给出 LocationId 而是整个字符串 <%#......%>正在弹出消息中。 如何在 javascript 中传递 Eval 值。
my link button -
<asp:LinkButton runat="server" ID="lbtnEdit" Text="edit" OnClientClick="javascript:msgDisp('<%# Eval(LocationId).toString() %>')" />
and the javascript msgDisp is-
<script type="text/javascript" language="javascript">
function msgDisp(lid) {
alert(lid);
}
</script>
but it is not giiving LocationId in pop but the whole string <%#......%> is comin in popup message. How can I pass Eval values in javascript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将
OnClientClick
的全部内容构建为代码括号内的字符串,它将按照您的预期输出。假设 LocationId 是一个有效的数字 - 在呈现时没有引号来包装您的值,因此输出类似
msgDisp(hello);
的内容将会中断。我不知道如何以这种方式解决这个问题,因此如果您必须这样做,我建议在ItemDataBound
事件期间设置OnClientClick
服务器端。以下是父级为Repeater
控件的情况。You can build the entire contents of
OnClientClick
as a string within the code brackets and it will output like you're expecting.This is assuming LocationId is a valid number- there are no quote marks to wrap your value when it renders, so outputting something like
msgDisp(hello);
is going to break. I don't know how to address that in this manner, so if you have to do that I would recommend settingOnClientClick
server side during theItemDataBound
event. Here's what it would like where the parent is aRepeater
control.如果您在标记中呈现了绑定表达式标签 (<%# ... %>),则意味着您的 LinkButton 未在绑定容器中初始化。正如 @lincolnk 所演示的,绑定容器可以是 Repeater 或 GridView 项、日历单元格等。此外,您不必在函数调用前添加“javascript:”前缀。 OnClientClick 属性的值呈现为锚点的 onclick 事件的处理程序。
If you are getting your binding expression tags (<%# ... %>) rendered in the markup, it means your LinkButton is not initialized in a binding container. A binding container can be, as @lincolnk demonstrated, an Repeater or GridView item, a Calendar cell, etc. Also, you do not have to prefix your function call with "javascript:". The value of the OnClientClick property is rendered as the handler of the anchor's onclick event.
网上到处找。每个人都说使用CodeBehind。
请参阅我的解决方案,即使我的数据值中像奥尼尔一样包含单引号,该解决方案也能正常工作。
如果您的数据项包含双引号,这将不起作用。但适用于我需要它做的事情,即以一个人的名义传递。
请注意警报调用中的反斜杠。
Looked everywhere on the net. Everyone says use CodeBehind.
See my solution, which works even when my datavalue has a single quote in it like O'Neal.
This will not work if your data item contains doublequotes. But works for what I needed it to do which was pass in a person's name.
Note the backslashes inside the alert call.
你可以这样做
OnClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>'
You can do like
OnClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>'
我要感谢林肯的回答。我目前正在帮助 googam.com 建立一个新的社交网络。我几天来一直在寻找一种解决方案,可以在数据列表中、jquery 模式对话框弹出窗口中查看用户的个人资料。在 ItemDataBound 事件中设置链接按钮 OnClientClick 解决了将用户 id 传递给 JQuery 函数以在弹出窗口中打开 acsx 用户控件的问题。
///////////////////////////
I want to thank lincolnk for his answer. I'm currently helping to build a new social network for googam.com. I have been searching for a few days for a solution to view a user's profile, in a datalist, in a jquery modal dialog popup. Setting the linkbutton OnClientClick in the ItemDataBound event solved the problem of passing the user id to the JQuery function to open a acsx user control in the popup window.
//////////////