passing data between (c#) and javascript

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

    Accepted Solution

    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

    LVL 13

    Expert Comment

    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

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

    Expert Comment

    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


    Featured Post

    Course: HTML5 Mobile App Development with PhoneGap

    PhoneGap can help you leverage your already existing HTML5, JavaScript, and CSS skills in order to create and deploy cross-platform mobile apps.This program comprehensively covers HTML5 mobile app development from top to bottom.

    Join & Write a Comment

    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now