How to call a client-side vbscript from server-side button

A

alan

Hi all,

I need to pass a variable to the client-side vbscript and when the button is
pressed invoke the script (SomeSub()).
The problem is, that the "blablabla" appears only after second pressing of
the button.
Probably there is something with the postback, but I cannot figure it out.

My .aspx :

<asp:Button id="Button3" runat="server" Text="Butt"></asp:Button>
<script language="VBscript" type="text/vbscript">
Sub SomeSub()
Dim mystr
mystr = "<%= variable %>"
document.write("variable")
End Sub
</script>

On my .vb file I'v got:

Public variable As String = "blablabla"

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Button3.Attributes.Add("onclick", "SomeSub()")
End Sub

Please help me.
 
G

Guest

Hi Alan

Yep, you're right. Your VBScript will always fire on the second click. This
is because your Attributes.Add occurs when the button is clicked, so you MUST
click the button in order for the VBs to be attached. You click it again,
it's attached, and it works!

To get that to work, get rid of the button handler (Private Sub
Button3_Click()) - unless you need it for something else of course, and add
the Attributes.Add to your page load...

Private Sub Page_Load()
Button3.Attributes.Add("onclick", "SomeSub()")
End Sub

Hope this helps,


Dan
 

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,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top