如何用jquery获取全局变量的值?

时间:2022-12-02 17:55:35

I've declared a static class named Global. In that class I've declared a static string variable LastID. I'm assigning values to this static variable in different web pages. Now I want to get the value of this Global variable in my .aspx page through jQuery. Can you suggest how I can get the value? In my Global class, code looks like this:

我声明了一个名为Global的静态类。在那个类中,我声明了一个静态字符串变量LastID。我在不同的web页面中为这个静态变量赋值。现在,我想通过jQuery在.aspx页面中获取这个全局变量的值。你能建议我怎样才能得到这个值吗?在我的全局类中,代码如下所示:

public static class Global
{
    static string lastID;
    public static string ImportantData
    {
        get
        {
            return lastID;
        }
        set
        {
            lastID = value;
        }
    }
}

I'm assigning value like this:

我这样分配值:

string LID = "MyID";
Global.ImportantData = LID;

Now I want to get this Global.ImportantData value with jQuery. How do I do that?

现在我想把它全球化。与jQuery ImportantData价值。我该怎么做呢?

4 个解决方案

#1


6  

You can do something like:

你可以这样做:

WebForms:

WebForms:

<script type="text/javascript">
    var importantData = '<%= Global.ImportantData %>';
</script>

Razor:

剃须刀:

<script type="text/javascript">
    var importantData = '@Global.ImportantData';
</script>

Be sure to fully qualify the namespace of Global unless you have included it in your Web.Config.

确保完全限定全局命名空间,除非您已经将它包含在Web.Config中。

Edit

编辑

In response to the comment, you can also assign it to a hidden field and just parse it out with jQuery as well:

作为对评论的响应,您还可以将其分配到一个隐藏字段,并使用jQuery解析它:

ASPX:

ASPX:

<asp:HiddenField ID="ImportantData" runat="server" />
...
<script type="text/javascript">
    var importantData = $("#<%= ImportantData.ClientID %>").val();
</script>

Code behind:

背后的代码:

protected void Page_Load (object sender, EventArgs e)
{
    ImportantData.Value = Global.ImportantData;
}

#2


0  

Use window.variablename or in jQuery style $.variablename to assign and access global variables

使用窗口。变量名或jQuery样式的$。分配和访问全局变量的变量名

#3


0  

you can use micro-data to store/read stuff from jQuery:

您可以使用微数据存储/读取jQuery的内容:

<input type="hidden" data-foo="bar" />

or you can produce JSON output by your ASP.NET (i.e. globals.aspx) page and than fetch it with ajax/json call and than parse/store/use by your script oh.. and you can use localStorage or cookies too.

也可以通过ASP生成JSON输出。NET(即globals.aspx)页面,并通过ajax/json调用获取,而不是解析/存储/使用您的脚本你也可以使用localStorage或cookie。

P.S. the last too solutions are less "spaghetti"

另外,最后一种解决方案也不那么“意大利面条”

#4


0  

In your Page:

在你的页面:

this.RegisterStartupScript("ImportantData", "var importantData= '" + Global.ImportantData + "';";

#1


6  

You can do something like:

你可以这样做:

WebForms:

WebForms:

<script type="text/javascript">
    var importantData = '<%= Global.ImportantData %>';
</script>

Razor:

剃须刀:

<script type="text/javascript">
    var importantData = '@Global.ImportantData';
</script>

Be sure to fully qualify the namespace of Global unless you have included it in your Web.Config.

确保完全限定全局命名空间,除非您已经将它包含在Web.Config中。

Edit

编辑

In response to the comment, you can also assign it to a hidden field and just parse it out with jQuery as well:

作为对评论的响应,您还可以将其分配到一个隐藏字段,并使用jQuery解析它:

ASPX:

ASPX:

<asp:HiddenField ID="ImportantData" runat="server" />
...
<script type="text/javascript">
    var importantData = $("#<%= ImportantData.ClientID %>").val();
</script>

Code behind:

背后的代码:

protected void Page_Load (object sender, EventArgs e)
{
    ImportantData.Value = Global.ImportantData;
}

#2


0  

Use window.variablename or in jQuery style $.variablename to assign and access global variables

使用窗口。变量名或jQuery样式的$。分配和访问全局变量的变量名

#3


0  

you can use micro-data to store/read stuff from jQuery:

您可以使用微数据存储/读取jQuery的内容:

<input type="hidden" data-foo="bar" />

or you can produce JSON output by your ASP.NET (i.e. globals.aspx) page and than fetch it with ajax/json call and than parse/store/use by your script oh.. and you can use localStorage or cookies too.

也可以通过ASP生成JSON输出。NET(即globals.aspx)页面,并通过ajax/json调用获取,而不是解析/存储/使用您的脚本你也可以使用localStorage或cookie。

P.S. the last too solutions are less "spaghetti"

另外,最后一种解决方案也不那么“意大利面条”

#4


0  

In your Page:

在你的页面:

this.RegisterStartupScript("ImportantData", "var importantData= '" + Global.ImportantData + "';";