Problem solve Get help with specific problems with your technologies, process and projects.

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.

Dig Deeper on Win Development Resources

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.

Please create a username to comment.