Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 ASP.NET
hi all,
 
how to create copy and paste button in c# asp.net web forms?
 
I am using this code:
 
private void btnCopy_Click(object sender, EventArgs e)
      {
          Clipboard.SetText(txtClipboard.Text);
      }
      private void btnPaste_Click(object sender, EventArgs e)
      {
          txtResult.Text = Clipboard.GetText();
      }
 
its running fine in IE but not in firefox.
 
I have no idea whats going wrong, plz help!!
Posted 15-Jul-11 2:39am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I found javascript code
 
Copy & Paste JavaScript Codes for popular browsers like IE,Firefox and opera etc[^]
 
EDIT
-----------------------------
Clipboard cut, copy and paste with JavaScript[^]
 
EDIT 2
-----------------------------
You right, found these links in web regarding security things.
Clipboard not working[^]
mozillazine - Clipboard not working[^]
I hate Firefox[^]
  Permalink  
v3
Comments
Uday P.Singh at 16-Jul-11 2:00am
   
this is only for copy, not for paste
thatraja at 16-Jul-11 11:30am
   
I'll update the answer
thatraja at 16-Jul-11 22:20pm
   
Hi Uday, check my updated answer.
Uday P.Singh at 17-Jul-11 2:56am
   
Yes, thatraja I have tried that, but it works only for IE, unfortunately FireFox doesn't allow Clipboard contents(copy/paste) for security reasons. BTW, thanks for your help!!
thatraja at 17-Jul-11 3:02am
   
Yep, you need to change security settings in firefox.
Anyway I'll update my answer again.
Uday P.Singh at 17-Jul-11 3:13am
   
I have tried all the stuffs on internet(including clipboard.swf) but nothing works on FireFox 5. then i found this:http://support.mozilla.com/en-US/kb/Granting%20JavaScript%20access%20to%20the%20clipboard , and I got the impression its not possible in FF 5.0
Espen Harlinn at 17-Jul-11 7:28am
   
Good effort - my 5
@OP: Pasting from clipboard using javascript is considered a security risk. User can still press Ctrl+V (or Shift+ins) . or use the menu
Uday P.Singh at 17-Jul-11 7:40am
   
Yeah I know that, but my client wants it to be done at paste button click(i guess you know what clients are and understand my issue)
Espen Harlinn at 17-Jul-11 7:46am
   
Certainly, but sometimes we have the dubious pleasure of educating our clients, usually an underappreciated effort :)
 
I'm fairly certain the client will not be happy if it turns out that later on the solution is used to breach the security ...
Uday P.Singh at 17-Jul-11 7:55am
   
the application is mainly used by a single person on the local intranet, hence security is a least issue!! BTW I convinced my client to use IE
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Why not take a look at the Clipboard jQuery plugin [^]
 
Best regards
Espen Harlinn
  Permalink  
Comments
thatraja at 17-Jul-11 7:31am
   
Proposing this as answer to OP(which is cross-browser). 5!
Espen Harlinn at 17-Jul-11 7:35am
   
Thank you, thatraja :)
Uday P.Singh at 17-Jul-11 7:49am
   
this no longer supports in adobe flash 10, BTW thanks for your help!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Fro what I understand, non-IE browsers are not clipboard friendly via source code. Since it appears as if you're using the clipboard contents within the same web site, why don't you just use a session variable instead?
  Permalink  
Comments
Uday P.Singh at 15-Jul-11 7:49am
   
no i am not using the clipboard contents within the same web site, for example what if some one copy content from notepad, then How can i use session to get the contents on copy?
John Simmons / outlaw programmer at 15-Jul-11 11:39am
   
In that case, you're kinda screwed.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Using the clipboard in a browser is a tricky business. Some browsers will not let you have direct access for security reasons, so you need to do some JavaScript hackery. I have had to dive into this in the past and how I managed to make it work is, when the user tries to paste, put the focus to an invisible* textarea, and then read the text out of it. Similarly, to copy you can select a range within the document and then queue a Ctrl+C on the document object.
 
(*: Invisible to the user, but it must be rendered, otherwise it won't receive the text. That means you can't use display:none or zero size. A good approach is to put it in a div which then gets hidden under some other control on your page.)
 
It is further complicated because IE does pasting in one way and the other browsers another.
 
Before you ask, no you can't have the code for it: I don't have it with me here, and it is copyright to the client for whom I wrote it. I hope this is enough information to get you started with finding how to do it yourself.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Using a session variable as suggested by John is
 
private void btnCopy_Click(object sender, EventArgs e)
{
  Session["MY_KEY"] = txtClipboard.Text;
}
 
private void btnPaste_Click(object sender, EventArgs e)
{
  txtResult.Text = Session["MY_KEY"] as string;
}
  Permalink  
Comments
Uday P.Singh at 15-Jul-11 9:31am
   
i am not using the clipboard contents within the same web site,for example what if some one copy content from notepad then How can i use session to get the contents on copy?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Your solution above would copy the text into the servers clipboard which isn't a good thing
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

  Permalink  
Comments
Mohamed Rasul at 9-May-13 6:21am
   
How to copy the text in 'hyperlink button' when we made a click over the same. Kindly reply ....................

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Zoltán Zörgő 220
1 Sergey Alexandrovich Kryukov 205
2 Peter Leow 160
3 BillWoodruff 85
4 bling 80
0 Sergey Alexandrovich Kryukov 9,543
1 OriginalGriff 6,901
2 Peter Leow 4,737
3 Zoltán Zörgő 4,404
4 CHill60 2,932


Advertise | Privacy | Mobile
Web01 | 2.8.150129.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100