passing data between (c#) and javascript

Is it possible to pass variables from to javascript and the other way around?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can "pass" a variable from into javascript like this

<script type="text/javascript">
var jsVariable = '<%=aspVariable%>';

To pass a variable from Javascript to, you would have to store the variable in a FORM, and then submit the form.
<script type="text/javascript">
function passToAspNet() {
    document.getElementById('formElementVar1').value = jsVariable;
<form id="formElement" method="POST" action="asppage.aspx">
     <input type="hidden" id="formElementVar1" value="" />

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
you can do that through hidden fields
Assign  value to Hidden Fileld which you can access on client side with Javascript like this
var s=document.getElementById('<%=hdnTest.ClientID%>')
so I need to populate a hidden form field with the c# variable and have javascript read it?
You have to pass the ClientID into a Javascript variable, then use that to get the form field by id.  The "HiddenFormField" is the ID of the field.  The field must be set to runat server.  I haven't tested this.  If you need more information open a new question:
<script type="text/javascript">
var jsFieldId = '<%=HiddenFormField.ClientID%>';
var jsField = document.getElementById(jsFieldId);
var jsFieldValue = jsField.value;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.