Search code examples
javascriptjqueryasp.netasp.net-mvczeroclipboard

ZeroClipboard for copy to clipboard in ASP.NET MVC


I want to add a copy-to-clipboard functionality to my ASP.NET webpage. I found ZeroClipboard, but I couldn't find any single example wroking. Can I make it work on local computer or do I need to upload to server to test it?

Please send me an example link.


Solution

  • jQuery ZeroClipBoard would probably be what you are looking for. ZeroClipBoard uses an invisible Adobe Flash movie for achieving clipboard functionality. We are using this in our project's and it is working absolutely fine.

    It is easy to implement. Download a Flash file and include it in the scripts folder and follow the below steps.

    1. Add jQuery and zClip to your document:

      <script type="text/javascript" src="js/jquery.js"></script>
      <script type="text/javascript" src="js/jquery.zclip.js"></script>
      
    2. Inside of a <script> block, attach zClip to the element which will become your "copy button":

      $(document).ready(function(){
          $('a#copy-description').zclip({
              path:'js/ZeroClipboard.swf',
              copy:$('p#description').text()
          });
          // The link with ID "copy-description" will copy
          // the text of the paragraph with ID "description"
          $('a#copy-dynamic').zclip({
              path:'js/ZeroClipboard.swf',
              copy:function(){return $('input#dynamic').val();}
          });
          // The link with ID "copy-dynamic" will copy the current value
          // of a dynamically changing input with the ID "dynamic"
      });