Reputation: 1916
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?
Upvotes: 5
Views: 35500
Reputation: 11
Clipboard.SetText(textBox1.Text.ToString()); Everyone forgot to tell you about .ToString() method. That works 100%
Upvotes: 1
Reputation: 121
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();
}
Upvotes: 12
Reputation: 305
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);
}
Upvotes: 2
Reputation: 3430
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.
Upvotes: 0
Reputation: 8079
In the Click Event of the button use the following:
Clipboard.SetText(textBox.Text);
Upvotes: 0