Search code examples
asp.nettextboxcopy-paste

asp TextBox with TextMode="Date" How to copy the date?


Is it possible to copy the date as a string to the clipboard? When we used DevExpress textbox, we were able to highlight entire date-field and copy. Now after we changed it to asp:TextBox (TextMode="Date"), we can't. The right-click context menu doesn't have any copy menu, and the left-click just displays the calendar. enter image description here

Any suggest to achieve it is appreciated.


Solution

  • Ok, so say this text box, and beside the text box, we have a button.

            <asp:TextBox ID="TextBox1" runat="server" TextMode="date" ClientIDMode="Static">
            </asp:TextBox>
             <button id="mycopy" type="button"
                onclick="copyt();return false"
                >Copy</button>
    
            <script>
    
                function copyt() {
                    console.log('start')
                    var mytext = document.querySelector('#TextBox1').value
                    navigator.clipboard.writeText(mytext)
                    alert("you can now ctrl-v -- copy text = " + mytext)
    
    
                }
    
            </script>
    

    So, the above will "copy" the contents of the text box into your paste buffer.

    Of course, we could I suppose if you have font-awesome, then have a button like this:

            <asp:TextBox ID="TextBox1" runat="server" TextMode="date" ClientIDMode="Static">
            </asp:TextBox>
             <button id="mycopy" type="button"
                onclick="copyt();return false"
                ><span class="fa fa-clipboard" aria-hidden="true"></span></button>
    
            <script>
    
                function copyt() {
                    console.log('start')
                    var mytext = document.querySelector('#TextBox1').value
                    navigator.clipboard.writeText(mytext)
                    alert("text = " + mytext)
    
    
                }
    

    The result is now this:

    enter image description here

    And to be fair, one probably should remove the "alert" command in above.

    As I suggested, another idea would be a custom right click context menu. However, that will require a good deal of code, and probably best to adopt some JavaScript library for this purpose. I did "look" for some examples, but the resulting code for a custom context menu was not all that clean. Hence, keeping this simple, the above looks to be about the least effort for the desired result.