How To Copy Text from a text box?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anni V
    New Member
    • Feb 2009
    • 22

    How To Copy Text from a text box?

    Could anyone please help me with the VB code in ASP.Net to copy the text from a textbox on the page to the clipboard by just clicking the Copy button placed beside it..........?
    Regards.......
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    That would have to be handled in javascript (or some other client-side script)

    I'll move your question to the correct forum.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      For IE, you can use clipboardData.s etData(). For Firefox, you will need to set permissions to allow the script to copy to the clipboard or sign the script. See Using_the_Clipb oard for more information. I've not checked the exact details for other browsers.

      Comment

      • Anni V
        New Member
        • Feb 2009
        • 22

        #4
        Hi
        I am unable to execute the same
        I have an asp.Net textbox txtrequests
        So I tried copying the text in that by using the txtrequests.tex t property
        on clicking the
        Code:
        <asp:Button ID="btnCopy" runat="server" Text="Copy" Height="" Width="59px"/>
        btnCopy but the same is still not working
        Could you please write the code (code behind language is VB) for the same & help me out asap

        Regards...
        Last edited by gits; Mar 6 '09, 09:37 AM. Reason: added code tags

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Note that this is a JavaScript problem, so post the client-side generated HTML code. You need to add an onclick event handler to deal with a user clicking the button:
          Code:
          <input type="button" ... onclick="someFunction()">
          where someFunction() is a JavaScript function which deals with the copying.

          Comment

          • Anni V
            New Member
            • Feb 2009
            • 22

            #6
            Originally posted by acoder
            Note that this is a JavaScript problem, so post the client-side generated HTML code. You need to add an onclick event handler to deal with a user clicking the button:
            Code:
            <input type="button" ... onclick="someFunction()">
            where someFunction() is a JavaScript function which deals with the copying.
            Thanks buddy,
            that was very helpful I managed it,
            thnk you

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              You're welcome. Glad you got it working :)

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Wait a second.
                When you have an ASP.NET button you have to be careful setting the onclick="someFu nction".

                For example if you create an ASP.NET Button and set it's onclick property:
                Code:
                <asp:Button ID="MyButton" runat="server" onclick="someFunction()" />
                The button will attempt to call a Server Side function named "someFuncti on" when it is clicked. This will not execute a JavaScript function named "someFuncti on".

                If you want to execute a JavaScript function during an onclick event you have to use the OnClientClick Property to do so:
                Code:
                <asp:Button ID="MyButton" runat="server" OnClientClick="someFunction()" />
                Normally I don't use the OnClientClick property. I usually set the JavaScript events for controls like buttons server side in either the Page Load or Page PreRender event:
                Code:
                MyButton.Attributes.Add("onclick","someFunction();")
                Please note that if you do not "return false" during a JavaScript onclick event, the button will submit the page to the server. When your page submits to the server, any client side (JavaScript) stuff done will be "undone" when the page is recreated in the browser....

                Therefore, it is "usually" a good idea to add "return false;" in your buttons.

                For example:

                (server code)
                Code:
                MyButton.Attributes.Add("onclick","someFunction(); return false;")
                (asp declaration code)
                Code:
                <asp:Button ID="MyButton" runat="server" OnClientClick="someFunction();return false;" />
                I honestly don't know how you got your ASP button to call a JavaScript function using the onclick property....

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  That's useful to know - thanks! I'll keep it in mind when there's an ASP.NET/JavaScript question here.

                  Comment

                  • Anni V
                    New Member
                    • Feb 2009
                    • 22

                    #10
                    Originally posted by Frinavale
                    Wait a second.
                    When you have an ASP.NET button you have to be careful setting the onclick="someFu nction".

                    For example if you create an ASP.NET Button and set it's onclick property:
                    Code:
                    <asp:Button ID="MyButton" runat="server" onclick="someFunction()" />
                    The button will attempt to call a Server Side function named "someFuncti on" when it is clicked. This will not execute a JavaScript function named "someFuncti on".

                    If you want to execute a JavaScript function during an onclick event you have to use the OnClientClick Property to do so:
                    Code:
                    <asp:Button ID="MyButton" runat="server" OnClientClick="someFunction()" />
                    Normally I don't use the OnClientClick property. I usually set the JavaScript events for controls like buttons server side in either the Page Load or Page PreRender event:
                    Code:
                    MyButton.Attributes.Add("onclick","someFunction();")
                    Please note that if you do not "return false" during a JavaScript onclick event, the button will submit the page to the server. When your page submits to the server, any client side (JavaScript) stuff done will be "undone" when the page is recreated in the browser....

                    Therefore, it is "usually" a good idea to add "return false;" in your buttons.

                    For example:

                    (server code)
                    Code:
                    MyButton.Attributes.Add("onclick","someFunction(); return false;")
                    (asp declaration code)
                    Code:
                    <asp:Button ID="MyButton" runat="server" OnClientClick="someFunction();return false;" />
                    I honestly don't know how you got your ASP button to call a JavaScript function using the onclick property....
                    Thanks Frinavale,
                    But I did not use the Onclick property exactly to make it work, I initially tried it but it did not work as you correctly stated, but then I did some r & d & found that the below code works
                    Code:
                    btnCopy.Attributes.Add("onclick","someFunction();")
                    but however I did not know the theory behind the return false statement & hence had not added that, thanks for your guidance I will implement it & check that out,
                    I apologise for making you guys believe that the Onclick worked, it was the attributes.add that had worked but I claimed it worked as my thought process was initiated from that suggestion.....
                    Anyways thanks a lot for the advice, it will improve my coding skills,
                    Thank you .....
                    Regards...

                    Comment

                    Working...