用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 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值