如何从 ASP.NET 变量中提供 JavaScript 变量数据?

发布于 2024-07-13 07:57:44 字数 228 浏览 16 评论 0原文

我已经为我们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

等你爱我 2024-07-20 07:57:44

可能最好最容易将它们公开为页面(或母版页,如果在每个页面上使用)的属性,并通过页面指令引用它们。

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

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

然后设置 Page_Load 上的值(或在母版页的 Page_Load 中)。

  public void Page_Load( object source, EventArgs e )
  {

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

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"];
        ...
  }
捎一片雪花 2024-07-20 07:57:44

这里提出的所有答案

var userID = '<%= UserID %>';

如果您嵌入的变量可以包含任意字符串数据,那么 都缺少一些重要的东西。 嵌入的字符串数据需要进行转义,这样如果它包含反斜杠、引号或不可打印的字符,它们就不会导致您的 Javascript 出错。

Rick Strahl 对此处所需的转义代码提出了一些建议。
使用 Rick 的代码,嵌入变量将如下所示:

var userId = <%= EncodeJsString(UserID) %>;

请注意,现在没有引号,Rick 的代码用引号将转义字符串括起来。

All the answers here that suggest something like

var userID = '<%= UserID %>';

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:

var userId = <%= EncodeJsString(UserID) %>;

Note that there are no quotes now, Rick's code wraps the escaped string with quotes.

残疾 2024-07-20 07:57:44

@tvanfosson的答案是我过去通常这样做的方式,但只是想想一些更优雅的东西。 我想我会把这个扔出去。

您可以在代码隐藏中设置一个 HashTable,并初始化一个 JavaScriptSerializer

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

然后像这样设置变量:

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

然后将变量序列化为页面中的 JavaScript:

<script type="text/javascript">
    var vars = <%=json.Serialize(jsvars)%>;
    alert(vars.name);
</script>

这可确保所有变量都正确转义,并最大限度地减少如果您需要使用大量变量,请编写代码。

@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:

<script type="text/javascript">
    var vars = <%=json.Serialize(jsvars)%>;
    alert(vars.name);
</script>

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

阿楠 2024-07-20 07:57:44

本文描述了最实用的解决方案针对您的问题的几种实用解决方案。

This article describes the most pragmatic solution several pragmatic solutions to your problem.

娇妻 2024-07-20 07:57:44

以下是您必须执行的步骤:

在代码隐藏页面中:

private int _userId;
private int _courseId;
protected int CourseId
{
  get { return _courseId;}
  set { _courseId = value;}
}
protected int UserId
{
 get { return _userId;}

set { _userId = value;}
}

步骤 2:根据您的要求,现在您必须设置这些属性。 问题是这些属性应该在从 JavaScript 引用之前设置。 也许在 Page_Load 事件中是这样的:

_userId = Session["userId"];
_courseId = Request.QueryString["CourseId"] != null ? Request.QueryString["CourseId"] : String.empty;

当然,您可以根据您的要求将它们解析为适当的类型。

最后,您可以在 JavaScript 中引用它们,如下所示:

var currentUserId = '<% = UserId %>';
var currentCouseId = '<% = CourseId %>';

这肯定可行。 我已经多次使用过这种方法。

Here are the steps that you will have to take:

In your code behind page:

private int _userId;
private int _courseId;
protected int CourseId
{
  get { return _courseId;}
  set { _courseId = value;}
}
protected int UserId
{
 get { return _userId;}

set { _userId = value;}
}

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:

_userId = Session["userId"];
_courseId = Request.QueryString["CourseId"] != null ? Request.QueryString["CourseId"] : String.empty;

Of course you can parse them to appropriate types based on your requirements.

Finally, you can reference them in JavaScript as follows:

var currentUserId = '<% = UserId %>';
var currentCouseId = '<% = CourseId %>';

This should definitely work. I have used this approach many times.

緦唸λ蓇 2024-07-20 07:57:44

将 .NET 服务器端数据传递到JavaScript,我列出了各种方法,包括:

  1. 通过发出 AJAX 请求来获取数据
  2. 通过外部 JavaScript 文件加载数据
  3. 使用 SignalR 打开持久连接
  4. 将数据附加到 HTML 元素
  5. 将数据直接分配给 JavaScript 变量
  6. 序列化 .NET 对象转换为 JavaScript 文字

In Passing .NET Server-Side Data to JavaScript, I list various approaches, including:

  1. Fetching Data by Making an AJAX Request
  2. Loading Data Through an External JavaScript File
  3. Opening a Persistent Connection with SignalR
  4. Attaching Data to HTML Elements
  5. Assigning Data Directly to a JavaScript Variable
  6. Serializing a .NET Object into a JavaScript Literal
剩一世无双 2024-07-20 07:57:44

您还可以使用 HTTP cookie。 如果您还需要向另一个方向传递值,那么这种方法就很好。

You can also use HTTP cookies. That approach is nice if you need to pass values the other direction as well.

椵侞 2024-07-20 07:57:44

我正在做同样的事情。 我只是在 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.

恋你朝朝暮暮 2024-07-20 07:57:44

我不确定 SCORM 或 .ashx 文件(没有经验),但就从 ASP.NET 获取值而言,您可以

var xyz = '<%= variable %>';

在 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

var xyz = '<%= variable %>';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文