Avatar of SystemSys
SystemSys
Flag for Saudi Arabia asked on

Copy link from GridView TO clipboard!

Greetings to you

i have GridView , i want when i press Label that name Copy . to do Copy this link to Clipboard
to explain what i want see the picture .. you will understand what i want

 Notice: the Label1 that name Copy i was but this tag <a href='some link'></a>

i don't want to be confuse , but i want to copy data when i press this label to Clipboard ..

thanks

Copy-link-to-Clipborad.jpg
ASP.NET

Avatar of undefined
Last Comment
rmuyargas

8/22/2022 - Mon
jmwheeler

Here is a link explaining how to copy something to the clipboard using javascript:

http://www.dynamic-tools.net/toolbox/copyToClipboard/
SystemSys

ASKER
hello
i want copy link from gridview ... same in the picture


jmwheeler

The picture doesn't help too much.  Can you post the code?  I can show you how to work the example I sent you into your code.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SystemSys

ASKER
Okey,,
I'll put my try code ,,, to copy link or value in Cell in GridView

i hope to see it ,, and find solutions

best regards



<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation ="false" 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 clipper() {
var data = document.getElementById("Label1").href;
 
alert("Clipboard = \n\n" + data);
window.clipboardData.setData("Text", data);
 
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="divGridView">
            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
                GridLines="None" AutoGenerateColumns="False" Width="251px" 
                onrowdatabound="GridView1_RowDataBound">
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                             <a href="javascript:clipper()"><asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label></a>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </div>
        <div>
        </div>
 
    </form>
</body>
</html>
===========================================================
this is code
 
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
public partial class _Default : System.Web.UI.Page 
{
    DataTable myTable;
    protected void Page_Load(object sender, EventArgs e)
    {
        myTable = new DataTable();
        myTable.Columns.Add("ID", typeof(Int32));
        myTable.Columns.Add("Name", typeof(String));
        myTable.Rows.Add(1, "aaa");
        myTable.Rows.Add(2, "bbb");
        myTable.Rows.Add(3, "ccc");
        GridView1.DataSource = myTable;
        GridView1.DataBind();
    }
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    Label lbl = e.Row.FindControl("Label2") as Label;
        //    Button btn = e.Row.FindControl("Button2") as Button;
        //    btn.Attributes.Add("onclick", "return CopyGridView('" + lbl.ClientID + "')");
        //}
    }
}

Open in new window

jmwheeler

Try this:

Replace your javascript with this:

<script type="text/javascript">
function clipper(value) {
alert("Clipboard = \n\n" + value);
window.clipboardData.setData("Text", value);
}
</script>

Then change your link to this:

<asp:LinkButton OnClientClick='clipper("<%# Eval("ID") %>");return false' Text='<%# Eval("ID") %>' runat="server" />
SystemSys

ASKER
this code dose not work ! i tell you i want copy value in gridview
did you test your code?

please make sure about that code ..
urgent

my best regards

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jmwheeler

Can you be more specific about how it doesn't work? Do you get some sort of error?  Do you see the alert but the data is not copied to the clipboard? etc.
SystemSys

ASKER
hmmm ..
i did not get any error , and i don't get any response , and did not copy it to clipboard

i put LinkButton  in gridview as template filed and edited script as you tell me
and also added code for LinkButton
<asp:LinkButton OnClientClick='clipper("<%# Eval("ID") %>");return false' Text='<%# Eval("ID") %>' runat="server" />


it's just display value in grid for field ( ID )

Please ,
jmwheeler

I think there are some problems with the quotes, try this.

<asp:LinkButton OnClientClick='clipper(<%# Eval("ID") %>);return false;' Text='<%# Eval("ID") %>' runat="server" />
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SystemSys

ASKER
unfortunately :(

nothing happened
when i press in the link just do refresh page !

i don't know ! Is my Question not clear ? or nobody do like this before ?
just i want copy link from gridView ..

please, help me Experts

my best regards



 
jmwheeler

Also, change the script

<script type="text/javascript">
function clipper(value) {
    clipboardData.setData("Text", value);
}
</script>
jmwheeler

What browser are you using?  If your using Internet Explorer you will probably see a yellow exclamation in the lower left corner of the browser after trying the copy.  This means there was a javascript error.  Double click the exclamation for error details.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SystemSys

ASKER
i'm using Mozilla Firefox

also .. code not work not response ..
please make small example and test it in your computer . if you see it's work ok then sent it to me ..

my best regards

jmwheeler

This is code that I have tested on my machine already, sorry if it is not working on your machine.
SystemSys

ASKER
Dear jmwheeler

i don't think that is problem with my machine ..

i don't have experience  in java script  but i think when you use something in inside gridview like label it's will change ID FOR This label , because it's inside gridview

your editor code in javascript  was not definition the button link

please review your code and make sure that inside gridview


my best regards


Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
jmwheeler

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SystemSys

ASKER
very goood excellent mr experts ....
rmuyargas

jmwheeler, this code works great, but gives an "Unterminated string constant" error when the value contains an apostrophe.  any ideas?