不能通过来访问JS/jQuery中的变量。

时间:2022-08-27 18:04:03

I'm trying to access a asp.net variable (c#) from JavaScript/jQuery.

我尝试从JavaScript/jQuery访问asp.net变量(c#)。

I've found a solution, here and here. But unfortunately these are not working for me.

我在这里和这里找到了一个解。但不幸的是,这些对我不起作用。

Here's a snippet:
Default.aspx.cs

这里有一个片段:Default.aspx.cs

public partial class Default : System.Web.UI.Page
{
    public string CurrentUser { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentUser = User.Identity.Name.Split('\\')[1]; //I need the value of "CurrentUser"
    }
    ...
}


script.js

script.js

$(document).ready(function (){
    var _currentUser = "<% =CurrentUser %>"; // e.g. _currentUser = "minimen"
    ...
});

_currentUser's value is always "<% =CurrentUser %>".

_currentUser的值总是“<% =CurrentUser %>”。

Any ideas?

什么好主意吗?

3 个解决方案

#1


2  

You should be injecting any values you need for client-side use into the page, not into the JavaScript. If you use clever script techniques (e.g. generate a JS file from a view), you void all possible caching of the "page" (script) which has serious implications for commercial deployment of a website.

应该将客户端需要的任何值注入到页面中,而不是JavaScript中。如果您使用了巧妙的脚本技术(例如,从视图生成一个JS文件),那么您将取消对“页面”(脚本)的所有可能的缓存,这将严重影响网站的商业部署。

Typically you would use an input type="hidden" or inject data- attribute values into a key DOM element (like body). You can pick them up very simply from JavaScript/jQuery:

通常,您将使用输入类型=“隐藏”或将数据属性值注入到一个关键的DOM元素(如body)中。您可以非常简单地从JavaScript/jQuery中获取它们:

1. Hidden field

e.g. for a hidden field with id="currentuser"

例如,对于id="currentuser"的隐藏字段

HTML:

HTML:

<input id="currentuser" type="hidden" value="<% =CurrentUser %>"/>

JQuery:

JQuery:

$(document).ready(function (){
    var _currentUser = $('#currentuser').val();
    ...
});

2. data- attribute

or add the value as a data- attribute on a key DOM element e.g. on the body element

或者将值添加为关键DOM元素的数据属性,例如在body元素上

HTML:

HTML:

<body data-currentuser="<% =CurrentUser %>">

JQuery:

JQuery:

$(document).ready(function (){
    var _currentUser = $('body').data('currentuser');
    ...
});

3. Inject global variable in the page

Worst-case, have a small snippet of Javascript at the top of the page that injects a global variable value:

最坏的情况是,在页面顶部插入一个小Javascript代码段,该代码段将注入全局变量值:

HTML:

HTML:

<script>
    window.currentuser="<% =CurrentUser %>";
</script>

JQuery:

JQuery:

$(document).ready(function (){
    var _currentUser = window.currentuser; // or just currentuser
    ...
});

I use this last technique in my Razor layout file for one simple purpose only. Injecting the site root URL on the current website, so that it is available for any relative Ajax calls:

我在我的Razor布局文件中使用了最后一种技术,目的很简单。在当前网站上注入站点根URL,以便对任何相对的Ajax调用都可用:

e.g. in Razor layout file

例如在剃刀布局文件中

<script>
    window.siteRoot="@Url.Content("~/")";
</script>

#2


3  

C# code in JS files is not executed - hence the value remains as <%= CurrentUser %>. To do as you require you either need to:

不执行JS文件中的c#代码—因此值保持为<%= CurrentUser %>。要按你的要求去做,你需要:

  • Execute this JS code within an ASPX page
  • 在一个ASPX页面中执行这个JS代码
  • Make an AJAX request to the server from the JS file which returns the required information.
  • 从返回所需信息的JS文件向服务器发出AJAX请求。

#3


0  

You cannot access the page fields from within a separate script file. Put your code in head element of Default.aspx and it will work.

不能从单独的脚本文件中访问页面字段。将代码放在head元素的默认值中。aspx,它会工作的。

#1


2  

You should be injecting any values you need for client-side use into the page, not into the JavaScript. If you use clever script techniques (e.g. generate a JS file from a view), you void all possible caching of the "page" (script) which has serious implications for commercial deployment of a website.

应该将客户端需要的任何值注入到页面中,而不是JavaScript中。如果您使用了巧妙的脚本技术(例如,从视图生成一个JS文件),那么您将取消对“页面”(脚本)的所有可能的缓存,这将严重影响网站的商业部署。

Typically you would use an input type="hidden" or inject data- attribute values into a key DOM element (like body). You can pick them up very simply from JavaScript/jQuery:

通常,您将使用输入类型=“隐藏”或将数据属性值注入到一个关键的DOM元素(如body)中。您可以非常简单地从JavaScript/jQuery中获取它们:

1. Hidden field

e.g. for a hidden field with id="currentuser"

例如,对于id="currentuser"的隐藏字段

HTML:

HTML:

<input id="currentuser" type="hidden" value="<% =CurrentUser %>"/>

JQuery:

JQuery:

$(document).ready(function (){
    var _currentUser = $('#currentuser').val();
    ...
});

2. data- attribute

or add the value as a data- attribute on a key DOM element e.g. on the body element

或者将值添加为关键DOM元素的数据属性,例如在body元素上

HTML:

HTML:

<body data-currentuser="<% =CurrentUser %>">

JQuery:

JQuery:

$(document).ready(function (){
    var _currentUser = $('body').data('currentuser');
    ...
});

3. Inject global variable in the page

Worst-case, have a small snippet of Javascript at the top of the page that injects a global variable value:

最坏的情况是,在页面顶部插入一个小Javascript代码段,该代码段将注入全局变量值:

HTML:

HTML:

<script>
    window.currentuser="<% =CurrentUser %>";
</script>

JQuery:

JQuery:

$(document).ready(function (){
    var _currentUser = window.currentuser; // or just currentuser
    ...
});

I use this last technique in my Razor layout file for one simple purpose only. Injecting the site root URL on the current website, so that it is available for any relative Ajax calls:

我在我的Razor布局文件中使用了最后一种技术,目的很简单。在当前网站上注入站点根URL,以便对任何相对的Ajax调用都可用:

e.g. in Razor layout file

例如在剃刀布局文件中

<script>
    window.siteRoot="@Url.Content("~/")";
</script>

#2


3  

C# code in JS files is not executed - hence the value remains as <%= CurrentUser %>. To do as you require you either need to:

不执行JS文件中的c#代码—因此值保持为<%= CurrentUser %>。要按你的要求去做,你需要:

  • Execute this JS code within an ASPX page
  • 在一个ASPX页面中执行这个JS代码
  • Make an AJAX request to the server from the JS file which returns the required information.
  • 从返回所需信息的JS文件向服务器发出AJAX请求。

#3


0  

You cannot access the page fields from within a separate script file. Put your code in head element of Default.aspx and it will work.

不能从单独的脚本文件中访问页面字段。将代码放在head元素的默认值中。aspx,它会工作的。