tatton777
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?
Here is the code:
//========================
// CopyEmailToClipboard()
//========================
protected void CopyEmailToClipboard()
{
Clipboard.SetText(txtEmail
}
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?
ASKER
I changed the code to...
protected void CopyEmailToClipboard()
{
Thread.CurrentThread.SetAp artmentSta te(System. Threading. ApartmentS tate.STA)
Clipboard.SetText(txtEmail .Text)
}
Now I am getting this error...
Failed to set the specified COM apartment state
protected void CopyEmailToClipboard()
{
Thread.CurrentThread.SetAp
Clipboard.SetText(txtEmail
}
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 ..........
Public Class abc <STAThread()> _ Shared Sub Main(ByVal args As String()) End SubEnd Class
Partial Public Class _Default
Inherits System.Web.UI.Page
.
.
.
.
.
End ..........
ASKER
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.
ASKER
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.InvalidOperationExc eption was unhandled by user code
Message="Failed to set the specified COM apartment state."
Source="mscorlib"
StackTrace:
at System.Threading.Thread.Se tApartment State(Apar tmentState state)
at menu_adm_user_password_mai ntenance.C opyEmailTo Clipboard( ) in d:\Bill's Docs\My Documents\Visual Studio 2008\WebSites\MySchoolFees AJAX 199137\menu_adm\user_passw ord_mainte nance.aspx .cs:line 248
at menu_adm_user_password_mai ntenance.b tnCopyToCl ipBoard_Cl ick(Object sender, EventArgs e) in d:\Bill's Docs\My Documents\Visual Studio 2008\WebSites\MySchoolFees AJAX 199137\menu_adm\user_passw ord_mainte nance.aspx .cs:line 231
at System.Web.UI.WebControls. Button.OnC lick(Event Args e)
at System.Web.UI.WebControls. Button.Rai sePostBack Event(Stri ng eventArgument)
at System.Web.UI.WebControls. Button.Sys tem.Web.UI .IPostBack EventHandl er.RaisePo stBackEven t(String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
at System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt)
InnerException:
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.
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.InvalidOperationExc
Message="Failed to set the specified COM apartment state."
Source="mscorlib"
StackTrace:
at System.Threading.Thread.Se
at menu_adm_user_password_mai
at menu_adm_user_password_mai
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.Process
InnerException:
I think we should wait for someone else. I don't want to misguide you. :)
ASKER
thanks for giving it a try! I do appreciate it. I am continuing to google. If I find the answer I will post it.
ASKER
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(o bject sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(CopyEmailToClipboard));
t.TrySetApartmentState(Apa rtmentStat e.STA);
t.Start();
t.Join();
}
[STAThread]
protected void CopyEmailToClipboard()
{
Clipboard.SetText(txtEmail ToUser.Tex t);
}
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(o
{
Thread t = new Thread(new ThreadStart(CopyEmailToClipboard));
t.TrySetApartmentState(Apa
t.Start();
t.Join();
}
[STAThread]
protected void CopyEmailToClipboard()
{
Clipboard.SetText(txtEmail
}
ASKER
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.
ASKER
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.
ASKER
Yay, this thread is back open! Hello, hello, hello, is there anybody out there...
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
http://www.velocityreviews.com/forums/t97080-paste-from-clipboard-in-asp-net.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
protected void CopyEmailToClipboard()
{
Thread.CurrentThread.SetAp
Clipboard.SetText(txtEmail
}