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

后端 未结 9 1069
野趣味
野趣味 2020-11-28 08:46

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 th

9条回答
  •  一整个雨季
    2020-11-28 09:12

    @tvanfosson's answer is how I have normally done this in the past, but was just trying to think of something a bit more elegant. Thought I'd throw this out there.

    You can set up a HashTable in the codebehind and also initialize a JavaScriptSerializer:

    Protected json As New System.Web.Script.Serialization.JavaScriptSerializer
    Protected jsvars As New Hashtable
    

    Then set variables like this:

    jsvars.Add("name", "value")
    

    Then serialize the variables into JavaScript in your page:

    
    

    This ensures that all variables are properly escaped and also minimizes the code if you need to work with a lot of variables.

提交回复
热议问题