-1

I'm working on a web application using asp.net and c# in the code behind. I just want to copy a text in a textbox to clipboard, my code is as follow:

The problem is that this is working properly in debug mode, but when deploying my website, in running mode the CopyToClipboard is not working!

Thread newThread = new Thread(new ThreadStart(CopyToClipboard));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();

private void CopyToClipboard()
{
    Clipboard.Clear();
    Clipboard.SetData("Text", txtEmailId.Text);
    Clipboard.SetText(txtEmailId.Text);
}
Jerodev
  • 29,019
  • 11
  • 72
  • 94
Samar
  • 47
  • 3
  • 8
  • 6
    Are you trying to copy to the clipboard on the server machine or the client machine? – HABJAN Oct 13 '14 at 09:46
  • 3
    If its part of an asp.net website, then the code you have posted will run on the server. If you are running in debug mode, your machines is the client and also the server so it appears to work, when you deploy it to an actual server, the code still works on the server, just not on the machine that is browsing the website. – Ben Robinson Oct 13 '14 at 09:49

1 Answers1

5

That Clipboard class resides in System.Windows.Forms. You cannot use this in ASP.net, because it will only attempt to modify the clipboard state of the server the ASP.net process runs on. And that's probably not what you're looking for.

To modify the clipboard from a website, you need to look at unreliable Javascript solutions. Please note that the clipboard is potentially unsafe and possible can't be accessed at all. Also see How do I copy to the clipboard in JavaScript?

Community
  • 1
  • 1
pyrocumulus
  • 8,296
  • 2
  • 37
  • 52