How to Select TextBox text in GridView and Copy to Clipboard with Javascript

Find the text of a TextBox control in an ASP.NET GridView and Copy to Clipboard with Javascript
by Updated September 19, 2018

Here's how you can find the text of a particular TextBox witnin an ASP.NET GridView row and then Select and Copy the text to Clipboard with Javascript.

First put the following javascript in the HTML of your page (either in HEAD of your html page or somewhere above the GridView control):

        <script type="text/javascript"> 
            //<![CDATA[

            function SelectTextandCopyToClipboard(currentTextBox) {

            var txtNameUrl;

            //Find the GridView Row using the TextBox reference.
            var row = currentTextBox.parentNode.parentNode;

            //Fetch all controls in GridView Row.
            var controls = row.getElementsByTagName("*");

            //Loop through the fetched controls.
            for (var i = 0; i < controls.length; i++) {

                //Find the TextBox control.
                if (controls[i].id.indexOf("RecommendUrlTextBox") != -1) {
                    txtNameUrl = controls[i];
                }
            }

            // Select text
            txtNameUrl.select();
            // Copy to clipboard
            document.execCommand('copy');

        }
         //]]>
    </script>  

 

Here's a sample GridView that corresponds to the Javascript above to Copy inner text from a single TextBox within the GridView

             <asp:GridView ID="LinksGridView" runat="server" OnRowDataBound="LinksGridView_RowDataBound"
                 DataKeyNames="LinkId" DataSourceID="LinksDataSource" PageSize="50">
                 <Columns>
                     <asp:BoundField DataField="LinkId" HeaderText="Id" InsertVisible="False"
                         ReadOnly="True" SortExpression="LinkId" />
                     <asp:TemplateField HeaderText="Link URL" HeaderStyle-HorizontalAlign="center">
                           <ItemTemplate>
                            <asp:TextBox ID="RecommendUrlTextBox" runat="server" Text="These words are will be selected." onclick="SelectTextandCopyToClipboard(this);" Width="200px" BackColor="#FDFEFF" ToolTip="Click to Copy URL"></asp:TextBox>
                          </ItemTemplate>
                     </asp:TemplateField>

                 </Columns>

 

Hope that helps...

For other similiar examples check out:

Find and validate TextBox and DropDownList controls in GridView row using JavaScript in ASP.Net

Get ASP.Net GridView Row and its RowIndex when clicked using JavaScript

You may also be interested in the book: JavaScript for .NET Developers

 


0
0

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Comment:  
Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts


Many modern websites today use Javascript and require it to be enabled in your browser for you to experience the full features of a site. Learn how to turn on or off Javascript in your Internet Explorer 6.x browser.  more »

You may need to have an image refreshed automatically on a web page in ASP.NET to get the latest image. One instance where you might want the fresh image is if you upload an image that has the same file name as an already existing image file on the...  more »

Here's how you can do a simple validation check for blank spaces in javascript. In the example below, the javascript function first trims the text value to make sure there is no extra white space and then checks the length for 0: function...  more »

The AjaxFileUpload control that's part of the AJAX Control Toolkit, works great for easily uploading multiple files at once. However, it gets a little tricky if you want to update an UpdatePanel after all the files have finished uploading, especially if...  more »

If you do any sort of ASP.NET programming there usually comes a time when you need to get a websites Base URL. The following shows two examples, the first example shows how to get the Base Site Url using C#, which can be used for getting both the...  more »

After running a ASP.NET website on IIS 7.5 for the first time on a Windows 7 computer, I was faced with the following error message: Login failed for user 'IIS APPPOOL\ASP.NET v4.0'. Description: An unhandled exception occurred during the execution of the...  more »

This is one of those simple web page design things that can drive a web developer absolutely crazy.  more »

Here's how to install Internet Information Services (IIS7) on a Windows 7 (or Vista) computer so that ASP.NET websites will run on the IIS7 web server. First, you will want to make sure that you are signed into an account with Administrator access on your...  more »

Here's how you can add "nofollow" tags to links generated by a Sitemap file that is bound to an ASP.NET Repeater control using a SiteMapDataSource. I'm currently using this technique for the footer links of GotKnowHow.com, so if you View Source of the...  more »

Here's how you can UrlEncode the plus sign (+) in a URL querystring in ASP.NET and then retrieve the plus symbol after UrlDecoding the string. In this example, I will do a postback and redirect the Server.UrlEncoded string to another page. First we will...  more »

In the code below, you will be able to find the baseUrl of your website using javascript. The following javascript code will work when used on your localhost or when it's used in a live site (finds the root url of the domain address). Just add this...  more »

So below I'm going to share with you a fairly easy to use and understand ASP.NET User Control that allows you to pick a Date (with the ajaxToolkit CalenderExtender) and also select the Time of day using a drop down list. I've named the control...  more »