在服务器的OnClientClick标签 [英] Server tag in OnClientClick

查看:95
本文介绍了在服务器的OnClientClick标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给我的一个错误服务器标签格式不正确

The following gives me an error of "The server tag is not well formed"

<asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" 
    OnClientClick="return confirm('Are you sure you want to delete <%# Eval("Username") %>?');">
    Delete
</asp:LinkButton>

(这是在数据绑定的ListView显示用户的列表中。当您单击删除按钮一个JavaScript确认对话框用来问你,如果你确定)

(This is used in a data bound ListView that displays a list of users. When you click the delete button a JavaScript confirm dialog is used to ask you if you're sure)

所以,我怎么能嵌入包含的JavaScript字符串中的服务器标签?

So, how can I embed a server tag in a string that contains JavaScript?

推荐答案

问题是绑定金块,并使用单引号和双引号的。

The problem is the binding nugget and the use of single and double quotes.

<asp:LinkButton D="DeleteButton" runat="server" CommandName="Delete" OnClientClick='<%# CreateConfirmation(Eval("Username")) %>'>Delete</asp:LinkButton>

然后在code-后面添加函数...

Then on the code-behind add the function...

Public Function CreateConfirmation(ByVal Username As String) As String
    Return String.Format("return confirm('Are you sure you want to delete {0}?');", Username)
End Function

当绑定金块被用作一个属性的值,你会注意到你必须使用单引号。您的脚本也需要嵌入式字符串参数的确认功能引号。你基本上用完了引号的。

When the binding nugget is used as the value for an attribute, you'll note you have to use single quotes. Your script also needed quotes for the embedded string parameter to the confirm function. You basically ran out of quotes.

这篇关于在服务器的OnClientClick标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆