Ask the Expert

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) {

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

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: