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 last 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.

Start the conversation

Send me notifications when other members comment.

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

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close