0

I have been through a lot of different Stack Overflow questions and answers which are related to this, not to mention tutorial videos, with no success. I need a button which copies a string to my clipboard. The below code is executed successfully when I click the button, but the string isn't copied. Any ideas why?

protected void copyToClipboard()
    {
        System.Windows.Forms.Clipboard.SetText("String to be copied");
    }

protected void Button_Click(object sender, EventArgs e)
{
    Thread clipboardThread = new Thread(copyToClipboard);
    clipboardThread.SetApartmentState(ApartmentState.STA);
    clipboardThread.IsBackground = false;
    clipboardThread.Start();
}

Thanks very much!

2 Answers2

0

With your code you are setting clipboard value on server side. If you wanted this to be done in browser (client side), you need to use JavaScript.

Mladen Oršolić
  • 1,282
  • 3
  • 20
  • 35
0

I suppose you want to copy the text to the clipboard of the client of your application. The .NET code you show runs on the server, so that the text is copied to the clipboard of the service account that runs the application on the server.

See this question and especially this answer on how to add text to the clipboard on the client in JavaScript.

Community
  • 1
  • 1
Markus
  • 15,389
  • 3
  • 25
  • 47
  • Ah! That makes sense. The code is working but I'm copying stuff to the clipboard on the server rather than the clipboard on the client machine. Thank you! – OnwardsAndUpwards Nov 16 '15 at 13:19