如何从 ASP.NET 变量中提供 JavaScript 变量数据?
我已经为我们的 LMS 创建了 SCORM API,现在我正在使用硬编码的 userID 和 courseID 变量(引用数据库中内容的变量)。 我需要传递真实的 userID 和 courseID,而不是使用硬编码的。 我知道 userID 存储在会话中,而 courseID 是从启动页面传递过来的。
如何将它们放入 JavaScript 中,以便将它们包含在对处理 SCORM 调用的 .ashx 的调用中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
可能
最好最容易将它们公开为页面(或母版页,如果在每个页面上使用)的属性,并通过页面指令引用它们。然后设置 Page_Load 上的值(或在母版页的 Page_Load 中)。
Probably
besteasiest to expose them as properties of your page (or master page if used on every page) and reference them via page directives.Then set the values on Page_Load (or in the Page_Load for the master page).
这里提出的所有答案
如果您嵌入的变量可以包含任意字符串数据,那么 都缺少一些重要的东西。 嵌入的字符串数据需要进行转义,这样如果它包含反斜杠、引号或不可打印的字符,它们就不会导致您的 Javascript 出错。
Rick Strahl 对此处所需的转义代码提出了一些建议。
使用 Rick 的代码,嵌入变量将如下所示:
请注意,现在没有引号,Rick 的代码用引号将转义字符串括起来。
All the answers here that suggest something like
are all missing something important if the variable you are embedded can contain arbitrary string data. The embedded string data needs to be escaped so that if it contains backslashes, quotes or unprintable characters they don't cause your Javascript to error.
Rick Strahl has some suggestions for the escaping code needed here.
Using Rick's code the embedded variable will look like this:
Note that there are no quotes now, Rick's code wraps the escaped string with quotes.
@tvanfosson的答案是我过去通常这样做的方式,但只是想想一些更优雅的东西。 我想我会把这个扔出去。
您可以在代码隐藏中设置一个 HashTable,并初始化一个
JavaScriptSerializer
:然后像这样设置变量:
然后将变量序列化为页面中的 JavaScript:
这可确保所有变量都正确转义,并最大限度地减少如果您需要使用大量变量,请编写代码。
@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
:Then set variables like this:
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.
本文描述了
最实用的解决方案针对您的问题的几种实用解决方案。This article describes
the most pragmatic solutionseveral pragmatic solutions to your problem.以下是您必须执行的步骤:
在代码隐藏页面中:
步骤 2:根据您的要求,现在您必须设置这些属性。 问题是这些属性应该在从 JavaScript 引用之前设置。 也许在
Page_Load
事件中是这样的:当然,您可以根据您的要求将它们解析为适当的类型。
最后,您可以在 JavaScript 中引用它们,如下所示:
这肯定可行。 我已经多次使用过这种方法。
Here are the steps that you will have to take:
In your code behind page:
Step 2 : based on your requirement now you have to set up those properties. The catch is that these properties should be set before they are referenced from the JavaScript. Maybe something like this in the
Page_Load
event:Of course you can parse them to appropriate types based on your requirements.
Finally, you can reference them in JavaScript as follows:
This should definitely work. I have used this approach many times.
在 将 .NET 服务器端数据传递到JavaScript,我列出了各种方法,包括:
In Passing .NET Server-Side Data to JavaScript, I list various approaches, including:
您还可以使用 HTTP cookie。 如果您还需要向另一个方向传递值,那么这种方法就很好。
You can also use HTTP cookies. That approach is nice if you need to pass values the other direction as well.
我正在做同样的事情。 我只是在 API 对象的构造函数中传递初始化时可能需要的任何值。 我没有看到任何关于如何创建 API 对象的要求,只是它如何位于 SCO 代码中。
I am in the process of doing the same thing. I am just passing whatever values I may need upon initialization and such in the API object's constructor. I have not seen any requirements as to how the API object can be created, just how it is located in the SCO's code.
我不确定 SCORM 或 .ashx 文件(没有经验),但就从 ASP.NET 获取值而言,您可以
在 ASP.NET 页面中执行各种操作。 有多种方法可以实现此目的,但最终结果是 ASP.NET 页面在发送到浏览器的 HTML(或 JavaScript)中呈现变量的值。
这是一个通用的 ASP.NET 和 JavaScript 答案,可能有更优雅的解决方案适合您。
I'm not sure about SCORM or .ashx files (no experience there) but as far as getting values from ASP.NET you can do all sorts of things like
from within your ASP.NET page. There are various ways to accomplish this but the end result is that the ASP.NET page renders the values of the variable in the HTML (or JavaScript) that is sent to the browser.
This is a generic ASP.NET and JavaScript answer, there may be a more elegant solution out there for you.