Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Copying the label text using LinkButton to clipboard says "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."
Here is my asp tag..
ASP.NET
<asp:Label ID="label" runat="server" Text="text to be copied"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server"  OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

Code-behind: C#
C#
protected void LinkButton1_Click(object sender, EventArgs e)
        { 
            Clipboard.SetText(label.Text);
        }

While click this LinkButton it shows error as I said above.. I could not use javascript / jquery in my application.. It is purely the server side web application. Please do post possible solution in c# itself.. If you all suggest me javascript clipboard, please provide me the solution that works out in all browser.
Posted
Updated 10-Dec-21 6:37am
v3
Comments
_Amy 20-Mar-13 2:54am    
Did you tried with separating threads? See my answer below.

Try creating a separate thread for that.
Try like this:
C#
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);
}



--Amit
 
Share this answer
 
v2
Comments
Janani Muthaiyan 20-Mar-13 2:56am    
I tried.. It is saying that "staThread.ApartmentState" have been deprecated..
_Amy 20-Mar-13 3:06am    
Try my updated solution. Working for me, and should work for you.
Janani Muthaiyan 20-Mar-13 3:11am    
ya, tried. but again the same problem exists..
_Amy 20-Mar-13 3:13am    
Check
Thread.SetApartmentState Method[^]
Thread.ApartmentState Property[^]
May be this could help you.

--Amit
Janani Muthaiyan 20-Mar-13 3:19am    
It worked.. I used like this.. Thanks alot...

public ApartmentState ApartmentState { get; set; }
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 newThread = new Thread(new ThreadStart(myMethod));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
}
public static void myMethod()
{
System.Windows.Forms.Clipboard.SetText(Val);
}
You do realize that even if that worked, it would copy to the clipboard in the Server, not the Client? Which is not going to help anyone, since you have no idea how many different users are simultaneously trying to do the same thing...

You can do it in Javascript: http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript[^] but I tend to view apps that corrupt my clipboard with deep suspicion, personally!
 
Share this answer
 
Comments
Janani Muthaiyan 19-Mar-13 7:48am    
@OriginalGriff.. I will be using session variable to maintain individual copies to each of the client. I just want to copy to the servers clipboard. It will be preferable if the error regarding [STA attribute] is solved...
OriginalGriff 19-Mar-13 8:18am    
The server only has the one clipboard - so if you copy anything to it from one client page, the next will overwrite it...and there is no guarantee that it will be preserved at all even if there is only one client at any time - anybody with access to the keyboard can overwrite it.

What are you doing that you think this is a good idea?
Janani Muthaiyan 19-Mar-13 8:20am    
I will be generating a random number which will be displayed in the label.. As soon it created it has to be copied when the user clicks the LinkButton.. He can paste it anywhere..
OriginalGriff 19-Mar-13 9:27am    
Not like that he can't - C# code is executed at the Server, not the Client, and saves to the Server clipboard, not the Client clipboard.
You need to run code on the Client to save to his clipboard - hence the Javascript link I gave you.
I just wanted to say that Solution 3 worked great. I used this to copy everything in a listBox so it could be pasted into a .txt file; of course you can paste it anywhere once in the clipboard.

A note here, remember to add Using System.Threading;
 
Share this answer
 
The ClipBoard is in Client Side so you can't copy it through Code Behind.

You have to use a javascript. This would work only in IE.

if your textbox id is txtEmail

var email = document.getElementById('txtEmail');

var emailValue = email.value;

//copy to clipboard
window.clipboardData.setData('Text' , emailValue );

Please note you can't change the 'Text' keyword.

// get the clipboard data
var emailText = window.clipboardData.getData('Text');

//clear clipboard data
window.clipboardData.clearData();

for more info visit http://msdn2.microsoft.com/en-us/library/ms535220.aspx
 
Share this answer
 
Comments
Janani Muthaiyan 19-Mar-13 7:42am    
@Rohit Kumar. I am in situation to not use javascript or jquery in my application
Rohit Kumar Mumbai 19-Mar-13 7:46am    
Try adding:
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

Immediately before your call to SetText.
Janani Muthaiyan 19-Mar-13 8:48am    
It throws exception like this "Failed to set the specified COM apartment state."

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