Solved

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

Posted on 2010-09-20
5
1,700 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…

749 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