C# 是否可以在 asp.net 页面中设置 localStorage 或 Session 变量并在另一个页面上的 javascript 中读取它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19561877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:18:05  来源:igfitidea点击:

Is it possible to set localStorage or Session variable in asp.net page and read it in javascript on the other page?

c#javascriptasp.netlocal-storagesession-variables

提问by mashet

As in question. Is it possible to set variable in asp.net page in localStorage and retrieve it on the other page?

正如所讨论的那样。是否可以在 localStorage 的 asp.net 页面中设置变量并在其他页面上检索它?

How to set localStorage variable in asp.net. Is it possible? After that I could read variable using:

如何在asp.net 中设置localStorage 变量。是否可以?之后,我可以使用以下方法读取变量:

localStorage.getItem('UserID');

采纳答案by Krzysztof Cieslak

I guess You can't. The whole point of local storage is that it is local and You can manipulate it only from javascript. If You need to pass values between server and client You need to use some transport technology - cookies, ajax calls, hidden fields etc. It will all depend on how your application is organized, what kind of information is being stored, its volume, whether you want to redirect or not, but in all cases this should be done using javascript since that's the only way to access data stored in localStorage.

我想你不能。本地存储的全部意义在于它是本地的,您只能从 javascript 操作它。如果您需要在服务器和客户端之间传递值,您需要使用一些传输技术 - cookie、ajax 调用、隐藏字段等。这一切都取决于您的应用程序的组织方式、存储的信息类型、其数量、是否您想重定向与否,但在所有情况下,这都应该使用 javascript 来完成,因为这是访问存储在 localStorage 中的数据的唯一方法。

回答by Vishwajeet Kulkarni

I think setting session variable in page & read variable in javascript of another page is possible. But, if you are looking out for localstorage that won't be. It needs javascript. For session variable e.g.
A.aspx

我认为在页面中设置会话变量并在另一个页面的 javascript 中读取变量是可能的。但是,如果您正在寻找本地存储,则不会。它需要javascript。对于会话变量,例如
A.aspx

<% Session["username"]="Donald Duck"; %>

<% Session["username"]="唐老鸭"; %>

B.aspx

.aspx

>  <script type="text/javascript">
>       var user = "<%= Session["username"] %>";
>       document.write(user);
>  </script>

回答by mashet

I've done this by using cookies:

我通过使用 cookie 做到了这一点:

Default.aspx.cs code behind:

Default.aspx.cs 后面的代码:

HttpCookie userIdCookie = new HttpCookie("UserID");
userIdCookie.Value = id.ToString();
Response.Cookies.Add(userIdCookie);
Response.Redirect("~/ImagePage.html");

HttpCookie Expires wasn't setted. It expires default with session.

未设置 HttpCookie Expires。它在会话中默认过期。

html page javascript:

html页面javascript:

function OnLoad() {
var userId = getCookie('UserdID');
if (userId == null)
    window.location = "http://localhost:53566/Default.aspx";        
}

function getCookie(cookieName) {
    var cookieValue = document.cookie;
    var cookieStart = cookieValue.indexOf(" " + cookieName + "=");
    if (cookieStart == -1) {
        cookieStart = cookieValue.indexOf("=");
    }
    if (cookieStart == -1) {
        cookieValue = null;
    }
    else {
        cookieStart = cookieValue.indexOf("=", cookieStart) + 1;
        var cookieEnd = cookieValue.indexOf(";", cookieStart);
        if (cookieEnd == -1) {
            cookieEnd = cookieValue.length;
        }
        cookieValue = unescape(cookieValue.substring(cookieStart, cookieEnd));
    }
    return cookieValue;
}

回答by Houdini Sutherland

Old post yes, but knowledge is always good.

旧帖子是的,但知识总是好的。

You can set the local or session storage from asp.net (indirectly). Since we can set up javascript code in asp.net and insert into the client side, there's no difference with the session or local storage.

您可以从 asp.net(间接)设置本地或会话存储。由于我们可以在asp.net中设置javascript代码并插入到客户端,因此与会话或本地存储没有区别。

Try this from server side

从服务器端试试这个

string script = string.Format("sessionStorage.userId= '{0}';", "12345");
ClientScript.RegisterClientScriptBlock(this.GetType(), "key", script, true);

That will set the session (you could do local) storage variable to the value 12345.

这会将会话(您可以执行本地)存储变量设置为值 12345。

回答by Mahdi Jafari

you can use asp:HiddenField

您可以使用 asp:HiddenField

after changed localstorageor sessionStorageyou fill hidden field and use in code behind.

更改后localstoragesessionStorage您填写隐藏字段并在后面的代码中使用。