Copy text to clipboard

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E11esar
    New Member
    • Nov 2008
    • 132

    Copy text to clipboard

    Hello there.

    Is there a corresponding class to the winforms ClipBoard in asp.net webforms please?

    I've been searching Google for some possible ideas but all solutions so far have been for WinForms.

    In short I have a TextBox and a Button and I want the button_click to copy the textBox.Text to the clipboard.

    Any ideas please?

    Thank you.

    M :)
  • E11esar
    New Member
    • Nov 2008
    • 132

    #2
    Solution

    Hi there.

    I have found out how to do this:

    Code:
    <script type="text/javascript">
    function CopyToClipboard()
    {
       document.Form1.txtArea.focus();
       document.Form1.txtArea.select(); 
       CopiedTxt = document.selection.createRange();
       CopiedTxt.execCommand("Copy");
    }
    </script>
    Thank you.

    M :)

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Are you sure that works in all browsers? I've never found a clipboard function that works across all the browsers.

      And I see that you've found the answer on your own, but I'd just like to remind you for the future, all ASP.NET code is executed on the server. That's why you had to use Javascript rather than .NET code, because the clipboard is client side. It's really easy to forget this and spend lots of time looking for a solution that doesn't exist.

      Comment

      Working...