5

I like to copy the text of the Textbox when user click button1, so that it can be paste anywhere.

I searched on google for some solutions, but didn't get any positive response.

Anybody advice me to how perform this action?

Reporter
  • 3,547
  • 5
  • 28
  • 45
Amrit Sharma
  • 1,724
  • 8
  • 41
  • 74

5 Answers5

11

You can use like this one:

private void btnCopy_Click(object sender, EventArgs e)
{
    Clipboard.SetText(txtClipboard.Text);
}
private void btnPaste_Click(object sender, EventArgs e)
{
    txtResult.Text = Clipboard.GetText();
}
Tiny Hacker
  • 121
  • 1
  • 6
2

You wish to copy text to the clipboard. The basic syntax is:

Clipboard.SetText("The text you want to copy");

But in order for it to work there is more work to put into it, use the links I provided. You can find further information here and here for c# and here for ASP.net which is more relevant for you.

This code was taken from CodeProject link aformentioned, and should work by using different thread.

private static string _Val;
public static string Val
{
    get { return _Val; }
    set { _Val = value; }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{            
    Val = label.Text;
    Thread staThread = new Thread(new ThreadStart (myMethod));
    staThread.ApartmentState = ApartmentState.STA;
    staThread.Start();
}
public static void myMethod()
{
    Clipboard.SetText(Val);
}
Community
  • 1
  • 1
avi.tavdi
  • 197
  • 2
  • 3
  • 17
  • Edited my answer with relevant link to ASP.net, the main syntax remains the same, other information is needed in order for it to work. Apparently, it is implemented differently in different browsers. – avi.tavdi Jun 14 '13 at 08:57
  • you should properly edit your code example to. Maybe show some code from the link you provided :) – Jens Kloster Jun 14 '13 at 09:10
1

Clipboard.SetText(textBox1.Text.ToString()); Everyone forgot to tell you about .ToString() method. That works 100%

0

In the Click Event of the button use the following:

Clipboard.SetText(textBox.Text);
Romano Zumbé
  • 7,568
  • 4
  • 28
  • 49
0

You must do this on the client side (your browser). Doing this in server side (ASP.NET) doesn't make sense.

Unfortunately, clipboard manipulation is not cross-browser. If you need it to be cross-browser, you have to use flash. Look at ZeroClipboard library.

Look at this jsfiddle for a working example.

<script type="text/javascript" src="http://www.steamdev.com/zclip/js/jquery.zclip.min.js"></script>
<a id='copy' href="#">Copy</a>
<div id='description'>this seems awesome</div>

$(document).ready(function(){
        $('a#copy').zclip({
            path:'http://www.steamdev.com/zclip/js/ZeroClipboard.swf',
            copy:$('div#description').text()
        });
});

Then for further examples on how to use ZeroClipboard, look at their md.

aiapatag
  • 3,135
  • 16
  • 22