Link to home
Start Free TrialLog in
Avatar of tatton777
tatton777Flag for United States of America

asked on

Copying to clipboard from an aspx page

Hello,

Here is the code:
  //================================================================
  // CopyEmailToClipboard()
  //================================================================
  protected void CopyEmailToClipboard()
  {
    Clipboard.SetText(txtEmail.Text);
  }

Here is the error:
Current thread must be set to single thread aparatment (STA) mode bbefore OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

I've read some threads on how to handle this but all the information I read says that I need to include code like this

[STAThred]
Main{}

As you know aspx pages don't have the Main method. How do I get around this?
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

Try like this:

  protected void  CopyEmailToClipboard()
  {
    Thread.CurrentThread.SetApartmentState(Threading.ApartmentState.STA)
    Clipboard.SetText(txtEmail.Text)
   }


Avatar of tatton777

ASKER

I changed the code to...
protected void  CopyEmailToClipboard()
 {
   Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA)
   Clipboard.SetText(txtEmail.Text)
  }

Now I am getting this error...
Failed to set the specified COM apartment state
Could you please add this in your default.aspx.vb or .cs

Public Class abc    <STAThread()> _    Shared Sub Main(ByVal args As String())    End SubEnd Class

Partial Public Class _Default
    Inherits System.Web.UI.Page
.
.
.
.
.
End ..........
I will add it to default.aspx but I was wondering if it might be better in the MasterPage?
Okay I don't know about any thing regarding your project. So, If possible for you, adjust it.


By default there is no Main() method in an asp.net project. However, I added on. Here is what the code looked like after I added the Main() method you suggested to index.aspx.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using UserSession;
using MySchoolFeesMethods;

public partial class index : System.Web.UI.Page
{
private SessionVariables sv = new SessionVariables();
private MSF_Methods MSF = new MSF_Methods();

[STAThread()]
public static void Main(string[] args)
{
}

protected void Page_Load(object sender, EventArgs e)
{
// Set up session variables

// Direct to certain page based on variables
}

After doing this, this is the error that was thrown
System.InvalidOperationException was unhandled by user code
Message="Failed to set the specified COM apartment state."
Source="mscorlib"
StackTrace:
at System.Threading.Thread.SetApartmentState(ApartmentState state)
at menu_adm_user_password_maintenance.CopyEmailToClipboard() in d:\Bill's Docs\My Documents\Visual Studio 2008\WebSites\MySchoolFees AJAX 199137\menu_adm\user_password_maintenance.aspx.cs:line 248
at menu_adm_user_password_maintenance.btnCopyToClipBoard_Click(Object sender, EventArgs e) in d:\Bill's Docs\My Documents\Visual Studio 2008\WebSites\MySchoolFees AJAX 199137\menu_adm\user_password_maintenance.aspx.cs:line 231
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
I think we should wait for someone else. I don't want to misguide you. :)
thanks for giving it a try! I do appreciate it. I am continuing to google. If I find the answer I will post it.
Here is how you do it...

It takes two methods to get the text into the clipboard.  The first method is your Click Method. The second method is the one that copies the text to the clipboard. You will notice that the name of the second method is referenced in the 1st line of the btnCopyToClipBoard_Click() method, I've bolded the place where you must reference the second method in the first. I hope that this is clear.

protected void btnCopyToClipBoard_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(CopyEmailToClipboard));
t.TrySetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
}

[STAThread]
protected void CopyEmailToClipboard()
{
Clipboard.SetText(txtEmailToUser.Text);
}
I thought that I had this problem whipped but I don't. When I run the web app locally it works great. However, when I publish it out to the web it fails.
Please don't close this thread. I thought that I had the problem figured out but the solution only works locally. Once I publish the site it fails. Thanks.
Yay, this thread is back open! Hello, hello, hello, is there anybody out there...
Avatar of puru1981
puru1981

Basically what you are trying to do will be working but actually that is on Server side so you will not get it on client machine. I hope this is your problem. since you are doing R&D on local so server and client are on same machine so it worked well. for client side you need to create an activex object and read and write from that activeX control. Below discussion might help you.

http://www.velocityreviews.com/forums/t97080-paste-from-clipboard-in-asp-net.html
ASKER CERTIFIED SOLUTION
Avatar of puru1981
puru1981

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial