在 C# 中复制到剪贴板 asp.net
copy to clipboard in c# asp.net
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>
截至 2021 年 12 月 - 这是一个使用 jQuery / js 的工作示例:
<script type="text/javascript">
function CopyToClipboard()
{
var copyText = $('[id$="txt_Output"]').val(); // txt_Output is the id of your textbox to copy from. Could refactor this to pass as a parameter as well if desired.
navigator.clipboard.writeText(copyText)
.then(() => { alert('Copied to clipboard.') })
.catch((error) => { alert('Copy failed. Error: ${error}') })
}
</script>
我从这个线程中拼凑了一些答案来帮助我得出上面的代码:
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>
截至 2021 年 12 月 - 这是一个使用 jQuery / js 的工作示例:
<script type="text/javascript">
function CopyToClipboard()
{
var copyText = $('[id$="txt_Output"]').val(); // txt_Output is the id of your textbox to copy from. Could refactor this to pass as a parameter as well if desired.
navigator.clipboard.writeText(copyText)
.then(() => { alert('Copied to clipboard.') })
.catch((error) => { alert('Copy failed. Error: ${error}') })
}
</script>
我从这个线程中拼凑了一些答案来帮助我得出上面的代码: