Server.Transfer方式(或称HttpContext方式)传值实例

        public   class   QueryPage   :   System.Web.UI.Page 
{ 
protected   System.Web.UI.WebControls.TextBox   txtStaDate; 
protected   System.Web.UI.WebControls.TextBox   txtEndDate; 
      ... 
///   <summary> 
///   开始时间 
///   </summary> 
public   string   StaDate 
{ 
get{   return   this.txtStaDate.Text;} 
set{this.txtStaDate.Text   =   value;} 
} 
///   <summary> 
///   结束时间 
///   </summary> 
public   string   EndDate 
{ 
get{   return   this.txtEndDate.Text;} 
set{this.txtEndDate.Text   =   value;} 
} 
.... 
private   void   btnEnter_Click(object   sender,   System.EventArgs   e) 
{ 
Server.Transfer( "ResultPage.aspx "); 
} 
} 
      
      在显示查询结果页面(ResultPage.aspx): 
        public   class   ResultPage   :   System.Web.UI.Page 
{ 
      private   void   Page_Load(object   sender,   System.EventArgs   e) 
      { 

//转换一下即可获得前一页面中输入的数据 
QueryPage   queryPage   =   (   QueryPage   )Context.Handler; 

Response.Write(   "StaDate: "   ); 
Response.Write(   queryPage.StaDate   ); 
Response.Write(   " <br/> EndDate: "   ); 
Response.Write(   queryPage.EndDate   ); 
    } 
} 

三、如果有许多查询页面共用一个结果页面的设置方法: 
        在这种方式中关键在于“   QueryPage   queryPage   =   (   QueryPage   )Context.Handler;   ”的转换,只有转换不依赖于特定的页面时即可实现。 
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作! 

1、先定义一个类,用该类放置所有查询参数: 
///   <summary> 
///   结果页面中要用到的值 
///   </summary> 
public   class   QueryParams 
{ 
private   string   staDate; 
private   string   endDate; 

///   <summary> 
///   开始时间 
///   </summary> 
public   string   StaDate 
{ 
get{   return   this.staDate;} 
set{this.staDate   =   value;} 
} 
///   <summary> 
///   结束时间 
///   </summary> 
public   string   EndDate 
{ 
get{   return   this.endDate;} 
set{this.endDate   =   value;} 
} 
} 

2、接口定义: 
///   <summary> 
///   定义查询接口。 
///   </summary> 
public   interface   IQueryParams 
{ 
///   <summary> 
///   参数 
///   </summary> 
QueryParams   Parameters{get;} 
} 

3、查询页面继承IQueryParams接口(QueryPage.aspx): 
        
///   <summary> 
///查询页面,继承接口 
///   </summary> 
public   class   QueryPage   :   System.Web.UI.Page,   IQueryParams 
{ 
protected   System.Web.UI.WebControls.TextBox   txtStaDate; 
protected   System.Web.UI.WebControls.TextBox   txtEndDate; 

private   QueryParams   queryParams; 
      ... 
///   <summary> 
///   结果页面用到的参数 
///   </summary> 
      public   QueryParams   Parameters 
{ 
get 
{ 
return   queryParams; 
} 
} 
.... 
private   void   btnEnter_Click(object   sender,   System.EventArgs   e) 
{ 
//赋值 
queryParams   =   new   QueryParams(); 
queryParams.StaDate   =   this.txtStaDate.Text; 
queryParams.EndDate   =   this.txtEndDate.Text 

Server.Transfer( "ResultPage.aspx "); 
} 
} 
4、别外的页面也如此设置 
5、接收页面(ResultPage.aspx): 
      
public   class   ResultPage   :   System.Web.UI.Page 
{ 
      private   void   Page_Load(object   sender,   System.EventArgs   e) 
      { 

QueryParams   queryParams   =   new   QueryParams(); 
IQueryParams   queryInterface; 
//实现该接口的页面 
if(   Context.Handler   is   IQueryParams) 
{ 
queryInterface   =   (   IQueryParams   )Context.Handler; 
queryParams   =   queryInterface.Parameters; 
} 

Response.Write(   "StaDate: "   ); 
Response.Write(   queryParams.StaDate   ); 
Response.Write(   " <br/> EndDate: "   ); 
Response.Write(   queryParams.EndDate   ); 
    } 
} 

 

转载于:https://www.cnblogs.com/tylertang/p/3391208.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值