Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I give JavaScript variables data from ASP.NET variables?

I have created a SCORM API for our LMS and right now I am using hard coded userID and courseID variables (variables that reference things in the database). I need to pass the real userID and courseID instead of using hard coded ones. I know the userID is stored in the session and the courseID is passed over from the launch page.

How do I get these into JavaScript so I can include them in my calls to the .ashx that handles the SCORM calls?

like image 451
MetaGuru Avatar asked Feb 16 '09 16:02

MetaGuru


People also ask

How can access JavaScript variable in ASP.NET code behind?

asp.net c# 2.0/3.5 To get JavaScript values in the code-behind, we need Hidden Input Control, whose runat attribute we will set to runat="Server" so that it can be accessible in code-behind. It will work as an intermediately to pass the value from client-side to server-side.

How do you assign a value to a variable in JavaScript?

You can assign a value to a variable using the = operator when you declare it or after the declaration and before accessing it. In the above example, the msg variable is declared first and then assigned a string value in the next statement.


1 Answers

Probably best easiest to expose them as properties of your page (or master page if used on every page) and reference them via page directives.

 <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"];         ...   } 
like image 200
tvanfosson Avatar answered Sep 30 '22 17:09

tvanfosson