ASP.NET 设置/获取/清空 用户的剪贴板(How to copy/get/clear value to clipboard)

时间:2021-08-10 23:55:46

We can Set/Get/Clear users’ clipboard by the JavaScript window.clipboardData object, which only works in Internet Explorer, by the following code:
 
window.clipboardData.setData("text",value)
window.clipboardData.getData("text")
window.clipboardData.clearData("text")
 
If you need set a code-behind value to user’s clipboard, please pass it to JavaScript variable first, and then, set the value to clipboard. Or you can call the JavaScript from code-behind, just like the following code:

 

protected   void  Button1_Click( object  sender, EventArgs e)
{
    
string  values  =   " hello world " ;
    ClientScript.RegisterStartupScript(
this .GetType(),  " copy " " window.clipboardData.setData('text',' " + values + " ') " true );
}


Thanks.