1

Pretty straightforward:

Clientside in javascript function, it is pretty simple to set a javascript variable to something in the code behind such as:

var testing = '<%= Me.SomePropInCodeBehind %>';

Can I do the opposite?

Something like : <% Me.SomePropInCodeBehind %> = "Foo";

1 Answer 1

3

It is not possible to set server side property like from javascript alone. You need to do postback or send ajax request to update the server side variable.

Server side code generates the html / javascript and send to the client. The value of property is set in html like a string literal and sent to client. Server side (asp.net) variable does not exists on client (javascript).

You can assign javascript value to some hidden field and get that hidden field on server to assign value to property if you want to do it through postback. If you want to do it with jQuery ajax you can pass pass the values in data in jQuery ajax() function. If you only need to update some value on server you better choose ajax to prevent whole page refresh.

Sign up to request clarification or add additional context in comments.

2 Comments

And just to add, most would consider Ajax to be preferable to a postback (unless you need to re-render the entire page, for some reason)
Thanks, I figured as much be appreciate the definitive answer. Will check as answer when allowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.