在ASP.NET-C中复制到剪贴板# [英] Copy to Clipboard in ASP.NET-C#

查看:60
本文介绍了在ASP.NET-C中复制到剪贴板#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用LinkBut​​ton将标签文本复制到剪贴板时说 在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。确保主函数上标有STAThreadAttribute。

这是我的asp标签..

Copying the label text using LinkButton to clipboard says "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."
Here is my asp tag..

<asp:Label ID="label" runat="server" Text="text to be copied"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server"  OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>



代码隐藏:C#


Code-behind: C#

protected void LinkButton1_Click(object sender, EventArgs e)
        { 
            Clipboard.SetText(label.Text);
        }



点击这个LinkBut​​ton它显示错误,如上所述..我无法在我的应用程序中使用javascript / jquery ..它纯粹是服务器侧网应用程序。请在c#中发布可能的解决方案..如果你们都建议我使用javascript剪贴板,请在所有浏览器中提供解决方案。


While click this LinkButton it shows error as I said above.. I could not use javascript / jquery in my application.. It is purely the server side web application. Please do post possible solution in c# itself.. If you all suggest me javascript clipboard, please provide me the solution that works out in all browser.

推荐答案

尝试创建一个单独的线程。

试试这样:

Try creating a separate thread for that.
Try like this:
private static string _Val;
public static string Val
{
    get { return _Val; }
    set { _Val = value; }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{            
    Val = label.Text;
    Thread staThread = new Thread(new ThreadStart (myMethod));
    staThread.ApartmentState = ApartmentState.STA;
    staThread.Start();
}
public static void myMethod()
{
    Clipboard.SetText(Val);
}







--Amit




--Amit


您确实意识到,即使它有效,它也会复制到服务器中的剪贴板,而不是客户端?这对任何人都没有帮助,因为你不知道有多少不同的用户同时尝试做同样的事情...



你可以在Javascript中做到这一点: http://stackoverflow.com/questions/400212/how-to -copy-to-the-clipboard-in-javascript [ ^ ]但我倾向于以个人的方式深深怀疑地查看损坏我的剪贴板的应用程序!
You do realize that even if that worked, it would copy to the clipboard in the Server, not the Client? Which is not going to help anyone, since you have no idea how many different users are simultaneously trying to do the same thing...

You can do it in Javascript: http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript[^] but I tend to view apps that corrupt my clipboard with deep suspicion, personally!


ClipBoard在客户端因此你不能通过Code Behind复制它。



你必须使用javascript。这只适用于IE。



如果你的文本框ID是txtEmail



var email = document。 getElementById(''txtEmail'');



var emailValue = email.value;



//复制到剪贴板

window.clipboardData.setData(''Text'',emailValue);



请注意你不能改变''Text''关键字。



//获取剪贴板数据

var emailText = window.clipboardData.getData(''Text '');



//清除剪贴板数据

window.clipboardData.clearData();



更多信息请访问http://msdn2.microsoft.com/en-us/library/ms535220.aspx
The ClipBoard is in Client Side so you can''t copy it through Code Behind.

You have to use a javascript. This would work only in IE.

if your textbox id is txtEmail

var email = document.getElementById(''txtEmail'');

var emailValue = email.value;

//copy to clipboard
window.clipboardData.setData(''Text'' , emailValue );

Please note you can''t change the ''Text'' keyword.

// get the clipboard data
var emailText = window.clipboardData.getData(''Text'');

//clear clipboard data
window.clipboardData.clearData();

for more info visit http://msdn2.microsoft.com/en-us/library/ms535220.aspx


这篇关于在ASP.NET-C中复制到剪贴板#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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