1

Is it possible to access public variables declared in the code behind from a JavaScript function, e.g.

var user = <%=User%>;

Where User is declared in the code behind as:

public string User = "";

And then set in the page_load event.

1
  • 1
    yes it is, if this code is contained in you asp.net page not in separate js file. and kindly add quotes in case of strings var user = '<%=User%>'; Commented Nov 13, 2013 at 10:11

1 Answer 1

3
<script type="text/javascript">
     var userID = '<%= UserID %>';
     var courseID = '<%= CourseID %>';

     .... more stuff....
 </script>

Then set the values on Page_Load (or in the Page_Load for the master page).

 public void Page_Load( object source, EventArgs e )
  {

        UserID = Session["userID"];
        CourseID = Session["courseID"];
        ...
  }

hope this will help you

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

1 Comment

Thank you! I'd missed the single quotes.

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.