简体   繁体   中英

How to copy text to clipboard from textbox in c# Web application

I am working on a website project. For that I need to copy the text from the textbox to the clipboard so that it can be pasted into a notepad or somewhere else. I am using Visual studio 2008 with c#.

I have written this code but it's not working:

<script language="javascript" type="text/javascript">
    function ClipBoard()
    {
        TextBox1.innerText = Button1.innerText;
        Copied = TextBox1.createTextRange();
        Copied.execCommand("RemoveFormat");
        Copied.execCommand("Copy");
    }
</script>
<asp:TextBox ID="TextBox1" runat="server">Click on the button to copy the this text</asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Copy Text" onclick="ClipBoard();" />

In this code 2 errors are occuring on the last line ")expected" and "invalid expresion term ')'"

Please help me if someone knows the solution for that.

Check the answer to the question linked by @Jon. Adding a little flash object to actually copy the text to clipboard seems the easiest way.

Something like this should do the trick

function copyIntoClipboard(text) {

var flashId = 'flashId-HKxmj5';

/* Replace this with your clipboard.swf location */
var clipboardSWF = 'http://appengine.bravo9.com/copy-into-clipboard/clipboard.swf';

if(!document.getElementById(flashId)) {
    var div = document.createElement('div');
    div.id = flashId;
    document.body.appendChild(div);
}
document.getElementById(flashId).innerHTML = '';
var content = '<embed src="' + 
    clipboardSWF +
    '" FlashVars="clipboard=' + encodeURIComponent(text) +
    '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
document.getElementById(flashId).innerHTML = content;
}

Humbly copied from Copy / Put text on the clipboard with FireFox, Safari and Chrome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM