Q

Is there a way to pass a variable from VB to my ASPx HTML JavaScript?

Is there a way to pass a variable from Visual Basic to my ASPx HTML JavaScript? In ASPx:

 function Button1_onclick() {
  
}
vb code -
Button1.Attributes.Add("onclick", "Button1_onclick()")
need a varible from vb code to be passed to button1_onclick()

I'm assuming "VB code" is code behind code, and the Button1_onclick() function is client-side JavaScript. If that is the case, to pass a server-side variable to the method, you only have to send the value with the event handler like this:

Button1.Attributes.Add("onclick", "Button1_onclick('" + var.ToString() + "')")

Now your JavaScript function should look like the following:

function Button1_onclick(serverValue) {
  //whatever
}

Remember that the new JavaScript onclick handler with the variable will be updated only the the page goes back to the server, which is when you server-side VB code is run anyways. There's no "live" connection between the server and client code.

This was first published in August 2003

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close