Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

it is such that I have a textbox where I have some content in, and when I click the button clicks then it copies the content that is in textbox by itself, it should not be such that you have to run your mouse over to copy it. it just has to be like that when you click on the button that copies the it themselves.

i have try its here:

C#
<asp:TextBox ID="TextBoxHenvis" CssClass="form-control input-lg" runat="server" ReadOnly="true"></asp:TextBox><br />
                <asp:Button ID="ButtonKopier" runat="server" Text="Kopier Link" OnClientClick="CopyToClipboard()" CssClass="btn btn-success" />


JavaScript
function CopyToClipboard() 
        { 
            var controlValue  = document.getElementById('<%=TextBoxHenvis.ClientID%>').value;
            alert(controlValue);
        }


This means in other words that it should download content and so when you click ctrl + v then throw the content into which you got by click button.
Posted
Updated 11-May-15 11:15am
v2

Please see this article: https://www.lucidchart.com/techblog/2014/12/02/definitive-guide-copying-pasting-javascript[^].

See also: http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript[^].

Note there is a pretty interesting trick described in the answer referenced above.

That said, browser make using clipboard pretty difficult, because automatic copy without the user consent it pretty dangerous; the user is unaware of that and can loose some data previously put to the clipboard. So, better don't do it at all.

Here is my advice: find any CodeProject code sample, say, your own code sample on this very page. Please click on "Copy Code". Are you getting the idea?
So, this is what I advise: use the CodeProject way: this is the simplest and at the same time the safest solution.

—SA
 
Share this answer
 
v2
Yes as Sergey says, you should tell the User that you are copying to clipboard while clicking the button or something.


Otherwise User would loose the existing clipboard data. So, change the button name like "copy to clipboard" or warn before copying.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900