locked
Copy to clipboard from an asp label which value is return from c# method RRS feed

  • Question

  • User1979988417 posted

    Please help, I'm able to get the copy to clipboard work with the following (from http://chadcarter30101.blogspot.com/2011/07/aspnet-copy-contents-to-clipboard.html)

    <textarea id='txtCopyToClipboard' rows="3">This text will copy to your clipboard</textarea><br />
    <input type="button" id='btnCopy' onclick="clipboardData.setData('Text',document.getElementById('txtCopyToClipboard').value);" value="Copy" />

    My dilema comes from trying to modify the above script to copy to clipboard from an ASP label:

    <asp:Label id="fullNameLabel" runat="server" Text='<%#FormatName_ForDisplay((string)Eval("FullName"))%>'></asp:Label>

    <input type="button" id='btnCopy' onclick="clipboardData.setData('Text',document.getElementById('fullNameLabel').value);" value="Copy" />

    FormatName_ForDisplay() return a string from C# method that format the full name in a certain way for display.  Can anyone lead me to the light to get the formated Full Name.  As of now it only return "fullNameLabel".  Thank you</div>

    Thursday, May 9, 2013 1:59 AM

Answers

  • User1814019480 posted

    hi,

    thanks for posting!

    I test you code like blew,and it work:

            <input type="button" id='btnCopy' onclick="getContent()" value="Copy" />
            <script>
                function getContent() {
                    var a = document.getElementById("fullNameLabel").innerHTML;
                    alert(a);
                    clipboardData.setData("Text", a);        
            }
            </script>

    it work well  in IE9.But I dont know your browser.

    About Browser compatibility problem, you can refer this link for more detial:

    http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard

    Hope this helps!


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2013 10:03 PM

All replies

  • User527778624 posted

    Hi,

    There is no value attr. on <Label> or <div> tag. use innerHTML

    onclick="clipboardData.setData('Text',document.getElementById('fullNameLabel').innerHTML);"

    Thursday, May 9, 2013 2:07 AM
  • User1979988417 posted

    It's not working for me, I have try your method by replacing the value to innerHTML, it still comes back as fullNameLabel, not the actual formated string, any other options?  Thanks

    Thursday, May 9, 2013 7:27 PM
  • User527778624 posted

    Hi,

    Could u post that formatted label innerHtml (the output you want be in clipboard)?

    and what format that fullNameLabel is surrounded with?

    and Where u r pasting the copied format data?

    Friday, May 10, 2013 2:59 AM
  • User1814019480 posted

    hi,

    thanks for posting!

    I test you code like blew,and it work:

            <input type="button" id='btnCopy' onclick="getContent()" value="Copy" />
            <script>
                function getContent() {
                    var a = document.getElementById("fullNameLabel").innerHTML;
                    alert(a);
                    clipboardData.setData("Text", a);        
            }
            </script>

    it work well  in IE9.But I dont know your browser.

    About Browser compatibility problem, you can refer this link for more detial:

    http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard

    Hope this helps!


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2013 10:03 PM
  • User1979988417 posted

    Thank you everyone for looking, I have to add validateRequest="false" on the page and also use IE for the top srcipt to work.  

    I ended up with this script:

    <script type="text/javascript">
    function select_all(obj) {
    var text_val = eval(obj);
    var append = 'Call ';
    text_val.focus();
    text_val.select();
    if (!document.all) return; // IE only
    r = text_val.createTextRange();
    r.execCommand('copy');

    }</script>

    and it's working for me.  If I want to append a text in front of this like 'Hello', how can I append to r ?  I tried var append = 'Hello'; r = append.concat(text_val.createTextRange());  but it's not working out for me.

    Sunday, May 26, 2013 5:19 PM