ASP.NET - Copy to Clipboard on Click of Gridview cell

Asked By Siva S on 19-May-10 03:17 AM
Hi,
I need to copy the content data on selection of any particular cell or row of column of gridview without any interface like buttons (For Ex: Click here to copy the content etc). Can anyone share your ideas?

Regards
Raji
Anand Malli replied to Siva S on 19-May-10 03:40 AM
Hi Siva

You can achive this things with little javascript and server side hack,Follow below procedure

1) First if all you need to bind javascript function to your cell in RowDataBound event like following

protected void GridView1_DataBound(object sender, GridViewRowEventArgs e) 
{ 
   if (e.Row.RowType == DataControlRowType.DataRow) 
   { 
e.Row.Cells[0].Attributes.Add("onclick","CopyToClipBoard(e.Row.Cells[0].ClientID);"); 
   } 
}

as you can see i have taken text of cell as .Text...now it depends on your need..you might have taken label inside you column so you will first use FindControl for that and take Text from it

2) Write Javascript like following along with that you need to add one hidden TextArea to your html,i am also writting same with javascript
<SCRIPT LANGUAGE="JavaScript">
  
function CopyToClipBoard(controlToFind) 
{
copiedText.innerText = document.GetElementById(textToBeCopied).value;
Copied = copiedText.createTextRange();
Copied.execCommand("Copy");
}
  
</SCRIPT> 
  
<body>
<TEXTAREA ID="copiedText" STYLE="display:none;">
</TEXTAREA>
  
</body>

now with this you would be able to copy the text to clipboard

i hope you got it

let me know from your side

thxs
Sergio replied to Siva S on 07-Mar-11 12:54 PM
I ended here on my search on the net for the solution shown in the topic. Must say that it does not work on gridviews that do consecutive databinds. P.Example a search page.

The first time the databound event executes it works Ok,
The second seems to throw a silent error due to adding an onclick event where it already exists.
The third time it crashes (for me that I do a find control) saying that the object (the control receiving the onclick) is nothing, and if you execute the above code as is it simply makes the column disappear out of the grid.

Still can't find a solution on this. Even when doing an initial Add and then modifying or removing always the event before the Add.
If somenone as a clue on how solve this, please drop a line here.

Thanks.
Sergio replied to Sergio on 07-Mar-11 01:16 PM
What I'm experiencing seems to only appear when creating and destroying columns dinamically.

On the first column I have an image button that is where I assign the onclick event to copy the value of the second column.
Then there are 5 more fixed columns (code, description, price, etc)
and then in the codebehind a search button, I first remove the columns after the fixed ones and then I recreate them and bound to their corresponding datafields on the datatable.

Thats something like:
Datatable load
remove columns from 6 to .... on the grid
Assign datasource to the gridview
Create columns from 6 to ... on the datatable
Databind.

If I comment the code for the dinamic columns the code behind starts to work over the 3th time I search.
Strange.