Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-20
5
Medium Priority
?
1,768 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 2000 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

Technology Partners: 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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