[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2008-11-11
Medium Priority
Last Modified: 2012-06-27
Is it possible to pass variables from asp.net to javascript and the other way around?
Question by:Mr_Shaw
  • 2
LVL 18

Accepted Solution

ChetOS82 earned 2000 total points
ID: 22932175
You can "pass" a variable from ASP.net into javascript like this

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

To pass a variable from Javascript to ASP.net, 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

LVL 13

Expert Comment

ID: 22932185
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%>')

Expert Comment

ID: 27619376
so I need to populate a hidden form field with the c# variable and have javascript read it?
LVL 18

Expert Comment

ID: 27619697
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;

Open in new window


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question