Link to home
Start Free TrialLog in
Avatar of Mr_Shaw
Mr_Shaw

asked on

passing data between asp.net (c#) and javascript

Is it possible to pass variables from asp.net to javascript and the other way around?
ASKER CERTIFIED SOLUTION
Avatar of ChetOS82
ChetOS82
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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 ASP.net 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;
alert(jsFieldValue);
</script>

Open in new window