Connecting Tech Pros Worldwide Forums | Help | Site Map

Can't Copy to Clipboard

SAL
Guest
 
Posts: n/a
#1: Feb 8 '06
I have the following VB.net Class/Function that I call from an ASP.net page:

Public Class MyTestClass
Public Function embedHyperlink(ByVal fileID As Integer, ByVal fileName
As String) As String

'Create a New Instance of the HyperLink Control
Dim createHyperLink As New System.Web.UI.WebControls.HyperLink

'Create an Instance of the ClipBoard Object
Dim objClipboard As System.Windows.Forms.Clipboard

Dim sBuildHyperLink As String

Try
'Set the Properties of the HyperLink Control
createHyperLink.ID = "EmbeddedHyperLink"
createHyperLink.NavigateUrl =
"http://www.someDomain.com/docPage.aspx?x=" & fileID
createHyperLink.Text = fileName

'Build HTML HyperLink String
sBuildHyperLink = "<a href=" & createHyperLink.NavigateUrl & ">"
& createHyperLink.Text & "</a>"

'Write HyperLink to Clip Board
objClipboard.SetDataObject(sBuildHyperLink, True)

Return sBuildHyperLink

Catch ex As Exception
Throw
Finally
createHyperLink = Nothing
objClipboard = Nothing
sBuildHyperLink = Nothing
End Try


End Function
End Class

When I get to the line "objClipboard.SetDataObject(sBuildHyperLink, True)",
that writes to the Windows Clipboard I get the following error and I don’t
know why:

Server Error in '/MyTestWeb' Application.
________________________________________
The current thread must set to Single Thread Apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Threading.ThreadStateException: The current thread
must set to Single Thread Apartment (STA) mode before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.

Can someone tell me what I’m doing wrong?

Thanks


Hans Kesting
Guest
 
Posts: n/a
#2: Feb 8 '06

re: Can't Copy to Clipboard


> I have the following VB.net Class/Function that I call from an ASP.net page:[color=blue]
>
> Public Class MyTestClass
> Public Function embedHyperlink(ByVal fileID As Integer, ByVal fileName
> As String) As String
>
> 'Create a New Instance of the HyperLink Control
> Dim createHyperLink As New System.Web.UI.WebControls.HyperLink
>
> 'Create an Instance of the ClipBoard Object
> Dim objClipboard As System.Windows.Forms.Clipboard
>
> Dim sBuildHyperLink As String
>
> Try
> 'Set the Properties of the HyperLink Control
> createHyperLink.ID = "EmbeddedHyperLink"
> createHyperLink.NavigateUrl =
> "http://www.someDomain.com/docPage.aspx?x=" & fileID
> createHyperLink.Text = fileName
>
> 'Build HTML HyperLink String
> sBuildHyperLink = "<a href=" & createHyperLink.NavigateUrl & ">"
> & createHyperLink.Text & "</a>"
>
> 'Write HyperLink to Clip Board
> objClipboard.SetDataObject(sBuildHyperLink, True)
>
> Return sBuildHyperLink
>
> Catch ex As Exception
> Throw
> Finally
> createHyperLink = Nothing
> objClipboard = Nothing
> sBuildHyperLink = Nothing
> End Try
>
>
> End Function
> End Class
>
> When I get to the line "objClipboard.SetDataObject(sBuildHyperLink, True)",
> that writes to the Windows Clipboard I get the following error and I don’t
> know why:
>
> Server Error in '/MyTestWeb' Application.
> ________________________________________
> The current thread must set to Single Thread Apartment (STA) mode before OLE
> calls can be made. Ensure that your Main function has STAThreadAttribute
> marked on it.
> Description: An unhandled exception occurred during the execution of the
> current web request. Please review the stack trace for more information about
> the error and where it originated in the code.
>
> Exception Details: System.Threading.ThreadStateException: The current thread
> must set to Single Thread Apartment (STA) mode before OLE calls can be made.
> Ensure that your Main function has STAThreadAttribute marked on it.
>
> Can someone tell me what I’m doing wrong?
>
> Thanks[/color]

What clipboard do you expect to copy to?
This looks like a portion of a code-behind file, which is executed on
the server. At the most you can write to the clipboard of the *server*,
not that of the *client*! And even that is problematic: only the
"aspnet" user would be able to access that particular clipboard item (I
think).

Hans Kesting


SAL
Guest
 
Posts: n/a
#3: Feb 8 '06

re: Can't Copy to Clipboard


Good point Hans. I do want it to copy to the Clipboard where the client is
and not the Server. Here is the code that calls my function now, but I still
get the same error:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim test As New MyTestClass

'Create an Instance of the ClipBoard Object
Dim objClipBoard As System.Windows.Forms.Clipboard

'Write HyperLink to Clip Board

objClipBoard.SetDataObject((test.CreateHyperlink(C Int(TextBox1.Text),
TextBox2.Text)), True)

End Sub

"Hans Kesting" wrote:
[color=blue][color=green]
> > I have the following VB.net Class/Function that I call from an ASP.net page:
> >
> > Public Class MyTestClass
> > Public Function embedHyperlink(ByVal fileID As Integer, ByVal fileName
> > As String) As String
> >
> > 'Create a New Instance of the HyperLink Control
> > Dim createHyperLink As New System.Web.UI.WebControls.HyperLink
> >
> > 'Create an Instance of the ClipBoard Object
> > Dim objClipboard As System.Windows.Forms.Clipboard
> >
> > Dim sBuildHyperLink As String
> >
> > Try
> > 'Set the Properties of the HyperLink Control
> > createHyperLink.ID = "EmbeddedHyperLink"
> > createHyperLink.NavigateUrl =
> > "http://www.someDomain.com/docPage.aspx?x=" & fileID
> > createHyperLink.Text = fileName
> >
> > 'Build HTML HyperLink String
> > sBuildHyperLink = "<a href=" & createHyperLink.NavigateUrl & ">"
> > & createHyperLink.Text & "</a>"
> >
> > 'Write HyperLink to Clip Board
> > objClipboard.SetDataObject(sBuildHyperLink, True)
> >
> > Return sBuildHyperLink
> >
> > Catch ex As Exception
> > Throw
> > Finally
> > createHyperLink = Nothing
> > objClipboard = Nothing
> > sBuildHyperLink = Nothing
> > End Try
> >
> >
> > End Function
> > End Class
> >
> > When I get to the line "objClipboard.SetDataObject(sBuildHyperLink, True)",
> > that writes to the Windows Clipboard I get the following error and I don’t
> > know why:
> >
> > Server Error in '/MyTestWeb' Application.
> > ________________________________________
> > The current thread must set to Single Thread Apartment (STA) mode before OLE
> > calls can be made. Ensure that your Main function has STAThreadAttribute
> > marked on it.
> > Description: An unhandled exception occurred during the execution of the
> > current web request. Please review the stack trace for more information about
> > the error and where it originated in the code.
> >
> > Exception Details: System.Threading.ThreadStateException: The current thread
> > must set to Single Thread Apartment (STA) mode before OLE calls can be made.
> > Ensure that your Main function has STAThreadAttribute marked on it.
> >
> > Can someone tell me what I’m doing wrong?
> >
> > Thanks[/color]
>
> What clipboard do you expect to copy to?
> This looks like a portion of a code-behind file, which is executed on
> the server. At the most you can write to the clipboard of the *server*,
> not that of the *client*! And even that is problematic: only the
> "aspnet" user would be able to access that particular clipboard item (I
> think).
>
> Hans Kesting
>
>
>[/color]
Mark Rae
Guest
 
Posts: n/a
#4: Feb 8 '06

re: Can't Copy to Clipboard


"SAL" <SAL@discussions.microsoft.com> wrote in message
news:93374043-358C-4F9D-940C-6562E99B4211@microsoft.com...
[color=blue]
> Can someone tell me what I'm doing wrong?[/color]

You're trying to get your server to write to your clients' clipboard - not
going to happen.

You can manipulate the clipboard in ASP.NET, but only through JavaScript,
and the text you're trying to copy to your clients' clipboard will need to
be sent down to the client as part of the JavaScript code...

http://www.google.com/search?sourcei...clipboard+copy


Closed Thread