c# - 在 c# asp.net 中复制到剪贴板

标签 c# asp.net clipboard

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms; // step 1

namespace School.Admin
{

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [STAThreadAttribute] // step 2
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Clipboard.SetText("Why it did not copy the words"); //step 3
    }
}

}

错误是:

在进行 OLE 调用之前,当前线程必须设置为单线程单元 (STA) 模式。确保选中 Main 函数

最佳答案

你应该使用一段js

  <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Copy to Clipboard</title>
     <script type="text/javascript">
         function CopyToClipboard(myID) 
         {
              var copyText = document.getElementById(myID);

              /* Select the text field */
              copyText.select();
              copyText.setSelectionRange(0, 99999); /* For mobile devices */

              /* Copy the text inside the text field */
              document.execCommand("copy");
         }
    </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Copy Text Box Text to Clip board" OnClientClick="CopyToClipboard('TextBox1')" />
        </div>
        </form>
    </body>
 </html>

关于c# - 在 c# asp.net 中复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68000750/

相关文章:

ASP.NET 成员(member)资格 - 用于跟踪附加信息的设计

python - Python 如何访问 X11 剪贴板?

google-chrome - 如何将文本从 Google Chrome 扩展程序复制到剪贴板?

c# - 如何将这 2 种方法重构为 1 种方法?

c# - 如何与 .net 中的另一个项目链接

c# - HttpClient在c#中将Host和UserAgent添加到请求中

c# - 如何以编程方式测量 azure web/worker 角色的内存使用情况?

c# - 查找在控件中具有属性的属性

c# - 无法加载文件或程序集 'System.Web.Http,' - 将一个项目添加到另一个解决方案时

Excel复制到剪贴板截断数据