ASP.Net页面保存持久数据的几种方法比较

之前写了一篇文章介绍Server.Transer()方法的使用及HttpContext类 , 这两天试验了使用Context.Items字典与Session对象在页面间传递值,或者保存持久数据,觉得有必要整理一下各自的特点.

Context字典与Session都可以保存任意的object,不同的是Context字典中的对象只在某个Http Request中有效,到了下一个Request的时候就无效了, 因此要使用Context字典传值,需要用Server.Transer()方法跳转到新页面, 新页面首次加载(!IsPostBack)时读取Context字典中的对象,在以后回发时, 因为Http Request不是来自先前的页面,而是来自新页面的请求, 因此并不能读取到Former Page中的Context字典数据.

FormerPage.aspx (类名FormerPage, ds是FormerPage类中的一个DataSet字段):

None.gif          private   void  btnToNewPage_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Context.Items[
"string"= "I am a string";//可用Context字典来存储信息, Transfer到新的页面后,首次加载新页面时,可以获取Context字典信息.
InBlock.gif
            Context.Items["contextData"= ds;// Context 可保存任意数据类型,Context字典特定于某个Http请求,对于不同客户端,值不一样
InBlock.gif

InBlock.gif            Application[
"appData"= ds;//Application可保存任何数据类型,对所有客户端有同样值,整个应用程序范围内有效
InBlock.gif

InBlock.gif            Session[
"sessionData"= ds;// Session可以保存任意的object类型,对不同客户端不同值
InBlock.gif

InBlock.gif            HttpCookie cookie 
= new HttpCookie("string","I am a string");  //cookie存储在客户端,只能保存字符串,对不同客户端有不同值    
InBlock.gif
            Response.Cookies.Add(cookie);            
InBlock.gif        
InBlock.gif
InBlock.gif            Server.Transfer(
"NewPage.aspx",false);                        
ExpandedBlockEnd.gif        }
上面的代码中页使用了Application和Cookie来保存页面持久数据,其特点在注释中已经注明. Application,Session,Cookie对象,可以在跟踪页面时查看集合中的数据或者数据名及类型.

方法是在Page_Load时设置Trace.IsEnabled = true;或者aspx页首行代码中指定:
<%@ Page language="c#" Trace="true" Codebehind="WebGridTest.aspx.cs" Inherits="WebGridTest" %>

NewPage.aspx中调用Context字典:
None.gif          private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {        
InBlock.gif            
InBlock.gif            
// Page Load at the first time:
InBlock.gif
            if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    FormerPage formerPage 
= (FormerPage)Context.Handler;            // Get Former Request Page    
InBlock.gif
                    DataSet ds = formerPage.GetDataSet();              // Invoke method of the Former Request Page
InBlock.gif
                    DataGrid1.DataSource = ds;
InBlock.gif                    DataGrid1.DataBind();
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Response.Write(
"Error get data from parent transfer page!");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Page Load of each time:        
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataSet dat 
= null;
InBlock.gif                
string str = string.Empty;
InBlock.gif
InBlock.gif                
if (Context.Items["string"]!=null)      //Get the "string" item in the Context Dictionary
InBlock.gif
                    str = (string)Context.Items["string"];
InBlock.gif                
if(Context.Items["contextData"]!=null)  //Get the "contextData" item in the Context Dictionary, it is a DataSet object
InBlock.gif
                    dat =  Context.Items["contextData"as DataSet;
InBlock.gif
InBlock.gif                
if(str!=null&&str!=string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//output the string:
InBlock.gif
                    Response.Write("<script>alert('string gotted:" + str + "');</script>"); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(dat!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//output the rows count of dataset
InBlock.gif
                    Response.Write("<script>alert('DataSet Rows Count:" + dat.Tables[0].Rows.Count + "');</script>");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception _ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert('Exception caught:" + _ex.Message + "');</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string path = Context.Request.Path; //Get the request file path
InBlock.gif
            Response.Write("<script>alert('Request From:" + path + "');</script>");
InBlock.gif            
ExpandedBlockEnd.gif        }

当点击FormerPage.aspx 的ToNewPage按钮时,跳转到NewPage.aspx, 此时浏览器显示的URL仍然是FormerPage.aspx, NewPage加载时会显示读取到的Context字典中的字符串值和dataset的记录数;
之后从新的浏览器窗口再次打开NewPage.aspx, 或者再NewPage页面点其他按钮,引起回发后, NewPage加载时并不能读取到FormerPage中定义的Context字典的数据,因为此时的请求来自NewPage,而非FormerPage

Reference:

Passing Server Control Values Between Pages

转载于:https://www.cnblogs.com/davidullua/archive/2005/07/28/201931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值