且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用会话变量将值从aspx页面传递到html页面?

更新时间:2023-02-11 22:47:57

希望在html页面中获得会话变量的值 [ ^ ]

Want to get value of session variable in html page[^]
引用:

由于HTML页面按原样提供,服务器上没有运行代码,因此无法访问会话。您需要将其设置为ASP.NET页面,或PHP页面,或其他页面正在运行的任何其他框架。然后您可以访问会话,与其他页面相同。

As a HTML page is served as is, with no code running on the server, it cannot access the session. You'd need to make it an ASP.NET page, or a PHP page, or whatever other framework your other pages are running in. Then you can access the Session, the same as with your other pages.


将要传递的值添加到cookie中的HTML页面并使用客户端脚本访问它
Add the value you want to pass to the HTML page in cookies and access it using client side script


这是使用ASP向响应中添加cookie的方法.NET C#



This is how you add cookies to response using ASP.NET C#

HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);





并使用以下Javascript读取cookie



And read the cookie using Javascript as below

    var ca = document.cookie.split(';');
    //Access value of font and color by using ca[0] and ca[1] respectively
}