GridView Column to Clipboard via a Button

G

Guest

I have searched all over and can not seem to find an effective way to simply
copy the contents of a ASP:gridview column to the clipboard based on a button
or similar HTML or ASP.NET control.

ASP 2.0 web application with VB.NET code developing in Visual Studio 2005 Pro.

Gridview has several columns, and users need the ability to copy one of the
columns to the clipboard for pasting into a separate app (that already has
the ability to paste multiple inputs in from the clipboard).

I have no problem creating another single-column gridview for this purpose,
or even copying the desired column's contents into some other object or array
to do this, but have not been able to find a decent example of copying to the
clipboard via a web form button or similar control.

Any examples or suggestions would be greatly appreciated.

As always, thanks in advance for your help!
 
W

Walter Wang [MSFT]

Hi JDRaven,

I'm not sure if I fully understood your question. Do you want to know how
to copy text to clipboard in JavaScript? or you need to know how to copy a
column of GridView?

For first question, this will depends on which browser, for IE, we can use
window.clipboardData.setData:

http://msdn2.microsoft.com/en-us/library/ms535220.aspx

For second question, since we're not able to register your own clipboard
format in JavaScript (we can only use text or url format), therefore I
think you will have to "encode" and "decode" the gridview column to/from
text so that the other side could paste the data correctly.

Please let me know more about your detailed requirement so that I can
further follow-up. Thanks.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Walter,

Thanks for the link. That got me started, but this turned out to be way
more complicated than I expected. But I did get it resolved after relying on
numerous other code snippets found around the web.

Basic solution involved the following:

1) Javascript that references your ASP.NET controls when executing
clientside and puts it into your clipboard (only works with IE):

<script type="text/javascript">
function CopyToClipBoard()
{
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text",
document.getElementById("tbMyTextBox").value);
alert("All " + document.getElementById("ddlRegion").value + " codes
have been copied to your local clipboard.");
}
else
{
alert("Internet Explorer required");
}
return false;
}
</script>

2) In PageLoad sub, adding:

btnCopyToClipBoard.Attributes.Add("onclick", "return
CopyToClipBoard();")

so that the Javascript function is called when the ASP button is clicked.

3) Adding some code to the sub that binds the GridView control to steal just
the column of data I need and hide it away for later use.

Since I only need to copy out a single column of data from the main
gridview, I made it easy on myself and created a hidden gridview based on the
same SQLDataSource that only contained the column to be put into the
Clipboard. The following code was then inserted to take the data out of the
gridview and put it into a multiline textbox control. The display of this
"copy" of the data from the column to be placed in the clipboard (if the
button is clicked) can not be hidden, or it will not render the control on
the page and the Javascript will then fail to find it, so I display it as a
very small little blip underneath my regular gridview. Basically, I
populated the textbox control using a manner such as:

'The following code block is in support of the Copy to ClipBoard
functionality
'The text box holding the values from the gridview needs to be
visible or else javascript
'will not be able to access it and use the control's value to
set clipboard
tbMyTextBox.Text = "" 'Empty it to start with
Dim i As Integer
'Cycle through the rows of the gridview, adding each value to
the textbox
For i = 0 To gvCopyToClipBoard.Rows.Count - 1
tbMyTextBox.Text = tbMyTextBox.Text +
ddlRegion.SelectedValue + gvCopyToClipBoard.Rows(i).Cells(0).Text +
Environment.NewLine
Next

Based on other postings I've read, it appears possible to actually get the
GridView data values while within the Javascript function by having it parse
the actual HTML table by the corresponding ID, but I was having trouble
getting it to do that so gave up and cheated by storing a copy of the data
possibly needed in a mostly hidden textbox control, whose value I had no
problem accessing in the script. Another advantage of storing all the column
values off in a corner is that if the gridview has paging turned on, and is
more than 1 page, the javascript would only have access to the data displayed
on that current page, and my users wanted all the values of this partilcular
column in their clipboard at once, and did not want to paste page by page.

The above snippets don't include my error handling and checking for
reasonable amounts of data, et al. And are just provided to save you some
time should you find yourself with a similar requirements.

As always, thank you all for your help and continued support.


"Walter Wang [MSFT]" said:
Hi JDRaven,

I'm not sure if I fully understood your question. Do you want to know how
to copy text to clipboard in JavaScript? or you need to know how to copy a
column of GridView?

For first question, this will depends on which browser, for IE, we can use
window.clipboardData.setData:

http://msdn2.microsoft.com/en-us/library/ms535220.aspx

For second question, since we're not able to register your own clipboard
format in JavaScript (we can only use text or url format), therefore I
think you will have to "encode" and "decode" the gridview column to/from
text so that the other side could paste the data correctly.

Please let me know more about your detailed requirement so that I can
further follow-up. Thanks.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi JDRaven,

Thanks very much for sharing your solution here. I'm sure this will benefit
the community a lot.

Just a side note, in case you're interested, have you considered exporting
the GridView to Excel and let your end-user copy/paste data from Excel?
This might be more easier for your user to use the data. (Just search
"GridView Export Excel" and you should be able to find more resources on it)

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks for the suggestion. That page already had an export to Excel feature
on it that they had been using for this purpose (and others), but wanted a
faster "one click" method for this purpose, which they use very frequently.
This allows them to get all the values into their clipboard without having to
export to excel, select the values (for a specific region), then copy. Will
probably save them 12-15 seconds an occurrence. They better use it a lot to
recover the 12 hours or so I put into researching and implementing this for
them ;o)

Again, thanks for the help, and have a great night!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top