Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi all,

how to create copy and paste button in c# asp.net web forms?

I am using this code:

C#
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

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[^]
 
Share this answer
 
v3
Comments
Uday P.Singh 16-Jul-11 2:00am    
this is only for copy, not for paste
thatraja 16-Jul-11 11:30am    
I'll update the answer
thatraja 16-Jul-11 22:20pm    
Hi Uday, check my updated answer.
Uday P.Singh 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 17-Jul-11 3:02am    
Yep, you need to change security settings in firefox.
Anyway I'll update my answer again.
Why not take a look at the Clipboard jQuery plugin [^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
thatraja 17-Jul-11 7:31am    
Proposing this as answer to OP(which is cross-browser). 5!
Espen Harlinn 17-Jul-11 7:35am    
Thank you, thatraja :)
Uday P.Singh 17-Jul-11 7:49am    
this no longer supports in adobe flash 10, BTW thanks for your help!!
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?
 
Share this answer
 
Comments
Uday P.Singh 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?
#realJSOP 15-Jul-11 11:39am    
In that case, you're kinda screwed.
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.
 
Share this answer
 
Using a session variable as suggested by John is

C#
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;
}
 
Share this answer
 
Comments
Uday P.Singh 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?
Your solution above would copy the text into the servers clipboard which isn't a good thing
 
Share this answer
 

 
Share this answer
 
Comments
Mohamed Rasul 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900