用JavaScript写Session的两种方法

方法一:

使用postback

复制代码
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private 
void Page_Load(object sender, System.EventArgs e)
    {
        
// Insure that the __doPostBack() JavaScript method is created
        this.ClientScript.GetPostBackEventReference(this, string.Empty);

        
if (this.IsPostBack)
        {
            string eventTarget 
= (this.Request["__EVENTTARGET"== null? string.Empty : this.Request["__EVENTTARGET"];
            string eventArgument 
= (this.Request["__EVENTARGUMENT"== null? string.Empty : this.Request["__EVENTARGUMENT"];

            
if (eventTarget == "SetSessionPostBack")
                
this.Session["SessionValue"= eventArgument;
        }
        
else
        {
            
this.Session["SessionValue"= "Original value";
        }

        
this.Response.Write("SessionValue: " + this.Session["SessionValue"].ToString() + "<br>");
    }


</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title></title>

    
<script type="text/javascript">
        
function setSessionValue(newValue) {
            __doPostBack(
'SetSessionPostBack', newValue);
        }
    
</script>

</head>
<body>
    
<form id="form1" runat="server">
    
<input id="Button1" type="button" value="button" onclick="setSessionValue('hello');" /><div>
    
</div>
    
</form>
</body>
</html>
复制代码

 

方法二:

使用AJAX

复制代码
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private 
void Page_Load(object sender, System.EventArgs e)
    {
        
if (!this.IsPostBack)
        {
            
this.Session["SessionValue"= "Original value";
        }

        
this.Response.Write("SessionValue: " + this.Session["SessionValue"].ToString() + "<br>");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title></title>

    
<script type="text/javascript">

        
function makeAjaxCall(webUrl, queryString) {
            
var xmlHttpObject = null;

            
try {
                
// Firefox, Opera 8.0+, Safari

                xmlHttpObject 
= new XMLHttpRequest();
            }
            
catch (ex) {
                
// Internet Explorer

                
try {
                    xmlHttpObject 
= new ActiveXObject('Msxml2.XMLHTTP');
                }
                
catch (ex) {
                    xmlHttpObject 
= new ActiveXObject('Microsoft.XMLHTTP');
                }
            }

            
if (xmlHttpObject == null) {
                window.alert(
'AJAX is not available in this browser');
                
return;
            }

            xmlHttpObject.open(
"GET", webUrl + queryString, false);
            xmlHttpObject.send();

            
var xmlText = xmlHttpObject.responseText;

            
return xmlText;
        }

        
function setSessionValue(newValue) {
            
var webUrl = 'AjaxPage.aspx';
            
var queryString = '?SessionValue=' + newValue;
            
var returnCode = makeAjaxCall(webUrl, queryString);
            
//alert(returnCode);
            <%= ClientScript.GetPostBackEventReference(this, string.Empty) %>;
        }

    
</script>

</head>
<body>
    
<form id="form1" runat="server">
    
<input id="Button1" type="button" value="button" onclick="setSessionValue('Lance Zhang');" /><div>
    
</div>
    
</form>
</body>
</html>
复制代码

 

方法二中,设置session需要一个额外的aspx页面:

 

复制代码
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private 
void Page_Load(object sender, System.EventArgs e)
    {
        string sessionValue 
= (this.Request["SessionValue"== null? string.Empty : this.Request["SessionValue"];
        string returnValue 
= "Sesson value changed to " + sessionValue;

        
this.Session["SessionValue"= sessionValue;

        
this.Response.ClearHeaders();
        
this.Response.Clear();
        
this.Response.Write(returnValue);
        
this.Response.End();
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
</div>
    
</form>
</body>
</html>
复制代码
posted @   LanceZhang  阅读(87648)  评论(9编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示