Solved

Cool GridView Cell Copy-To-ClipBoard Missing ASP Code?

Posted on 2010-09-20
5
1,631 Views
Last Modified: 2012-05-10
Very nice coding, but unsure about front-end ASP code.

Q. Does it use a ASP:Hyperlink or Button Column?

Q. Does ClientID refer to the data going to the ClipBoard?

http://www.eggheadcafe.com/community/aspnet/17/10171172/copy-to-clipboard-on-click-of-gridview-cell.aspx
0
Comment
Question by:pointeman
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33726509
1) It can be either a HyperLink or a Button column, since they both have onClick attribute.

2) ClientID refers to the control inside the first cell (e.Row.Cells[0].ClientID).  If the control is not the first cell, then you need to change the index into the Cells property.
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 33726655
Q. Does it use a ASP:Hyperlink or Button Column?
The onclick event can be any control, but it might be more logical if it were a link or button.

Q. Does ClientID refer to the data going to the ClipBoard?
The ClientID is the ID used by the JavaScript client to find the controls on the HTML document.

Here is a complete working example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
          
        function CopyToClipBoard(controlToFind) 
        {
            var copiedText=document.getElementById('copiedText');
            copiedText.innerText = document.getElementById(controlToFind).innerText;
            Copied = copiedText.createTextRange();
            Copied.execCommand("Copy");
        } 
    </script>
    <script runat="server">
        protected void testGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Attributes.Add("onclick", "CopyToClipBoard('" + e.Row.Cells[0].ClientID + "');");
                e.Row.Cells[1].Attributes.Add("onclick", "CopyToClipBoard('" + e.Row.Cells[1].ClientID + "');");
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <textarea id="copiedText" style="display: none;" rows="5" cols="10"></textarea>
        <div>
            Click on the Value cell to copy the color hex value into the clipboard.<br />
            <asp:GridView ID="testGrid" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1" OnRowDataBound="testGrid_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                    <asp:BoundField DataField="value" HeaderText="value" SortExpression="value" />
                </Columns>
            </asp:GridView>
            <asp:XmlDataSource ID="XmlDataSource1" runat="server">
                <Data>
                    <colors>
                        <color name="Red" value="#FF0000"/>
                        <color name="Green" value="#00FF00"/>
                        <color name="Blue" value="#0000FF"/>
                        <color name="Cyan" value="#00FFFF"/>
                        <color name="Purple" value="#FF00FF"/>
                        <color name="Yellow" value="#FFFF00"/>
                    </colors>
                </Data>
            </asp:XmlDataSource>
        </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Comment

by:pointeman
ID: 33726904
Q. Where does "ClientID" fit into your datagrid or xml code?
I'm pulling data from SQL data base.
 
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 33727072
The XML is the datasource for the grid.  You can use any datasource and the code should work.

The ClientID is the name used to find the control in JavaScript using the document.getElementById() function.
0
 

Author Comment

by:pointeman
ID: 33739152
Still not working, I'll get back here soon, busy now...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now