Calling a client side script from server side.

G

Guest

Hi, I have an ASP.NET solution, and the ASPX page I have a form , I want to
copy some of the data from that form to the clipboard, I am using the below
script the script works fine when I use a normal anchor tag with the onclick
event, but I want to be able to use an actual asp control like the hylperlink
or linkbutton. I have tried the link button but it gives an error saying
Compiler Error Message: BC30456: 'VBScript' is not a member of 'ASP.sr_aspx'.
And the hyperlink control doesn't seem to have an option for the onclick. The
code is located in the .aspx not in the .vb. Thanks!

<script language="vbscript" type="text/VBScript">
sub CopyToClip
dim mytext
mytext = document.Form1.txtNote.Value
call window.clipboardData.setData("Text", mytext)
end sub
</script>
 
L

Lucas Tam

The
code is located in the .aspx not in the .vb.

You can't call client side scripting from the server side. You'll need
fudge some type of onClick event - on the client side to execute the
script.
 
J

Juno

Hi Dustin II,

You could just add onclick to hyperlink. <a onclick="CopyToClip()"
href="#">your text</a>
Do not use link button ,because when clicking a link button, there is always
a postback,. It is a button.
 
L

Levi Rosol

Calling client side scripts from server side code is very possible, and easy
to do. First, you need to add the script to your page. To do this, you need
to add the following to your code behind:

Dim sb as System.Text.StringBuilder = new System.Text.StringBuilder
sb.append("<script language='vbscript' type='text/VBScript'> ")
sb.append("sub CopyToClip ")
sb.append("dim mytext ")
sb.append("mytext = document.Form1.txtNote.Value ")
sb.append("call window.clipboardData.setData('Text', mytext) ")
sb.append("end sub ")
sb.append("</script> ")

Mybase.Page.RegisterStartupScript("myscript",sb.ToString())

Make sure to remove that script from your aspx page. if you build at this
point, when you view src, you should see your script
just as you did before.

Now to add it to, say and img, onClick event.

assuming you have an image called img declared with the protected withevents
syntax, do the following

img.Attributes.add("click") = "CopyToClip"



after typing up this response and rereading it, really, the only thing you
need to do is add the img.Attributes.add("click") = "CopyToClip" line to
your code
behind.

look at this as a two for one special. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top