Solved

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

Posted on 2010-09-20
5
1,693 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

827 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