Solved

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

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

632 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