c# - ASP :textbox select all text by click button and copy to client clipboard

标签 c# asp.net textbox multiline

有没有什么方法可以选择多行 asp:textbox 中的所有文本,并使用 C# 通过单击按钮将其复制到客户端剪贴板?

提前谢谢你。

最佳答案

您可以使用 document.execCommand("copy"); 请注意,大多数新浏览器都支持此功能,据我所知,不支持 Safari:

 <head runat="server">
    <title></title>
    <script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnCopy").click(function () {
                var id = "#" + "<%= txtText.ClientID %>";
                try {
                    $(id).select();
                    document.execCommand("copy");
                }
                catch (e) {
                    alert('Copy operation failed');
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="txtText" runat="server" Text="Some sample text to copy"></asp:TextBox>
        <button id="btnCopy">Copy</button>
    </form>
</body>

经过测试并适用于以下浏览器:

  1. IE 11 及更高版本
  2. 谷歌浏览器 51.0.2704.84
  3. 火狐 43.0.1

关于c# - ASP :textbox select all text by click button and copy to client clipboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37784995/

相关文章:

c# - 如何制作只接受数字的文本框?

c# - 在文本框中显示列表<String> (Winforms)

c#如何在foreach语句中获取最后一次?

c# - EmguCV 中的多层感知器

asp.net - 服务器.MapPath ("."),服务器.MapPath ("~"),服务器.MapPath(@"\"),服务器.MapPath ("/")。有什么不同?

asp.net - 在启动时获取 azure 应用服务插槽名称?

asp.net - WebForms 身份验证作为 MVC 过滤器

c# - 从 ASP.NET 执行 SSIS 包时出错

html - 锁定文本框的宽度

c# - 文本框中的换行符