ASP.NET 传参

ASP.NET Server.Transfer()详解

ASP.NET Server.Transfer()是在两个页面之间进行传值的好方法,从A页面Transfer到B页面时,就可以在B页面通过Context.Handler获得A页面的一个类的实例,从而在B调用A的各个成员对象。

下面的示例建立了WebForm1和WebForm2,通过Server.Transfer()方法演示在WebForm2中读取WebForm1的文本框、读取属性、通过Context传值、调用WebForm1的方法等:

WebForm1上放置一个TextBox1和一个Button1,程序如下:

 补充说明,就是Transfer方法的第二个参数指示是否保留页面的Form和QuerryString的值,你可以试着把它设为False,则在WebForm2中将读不到TextBox1的值。

ExpandedBlockStart.gifContractedBlock.gifpublic Class WebForm1class WebForm1 : System.Web.UI.Page
InBlock.gif {
InBlock.gif  
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif  
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif 
InBlock.gif  
private void Page_Load(object sender, System.EventArgs e)
InBlock.gif  {
InBlock.gif   Context.Items.Add(
"Context","Context from Form1");
InBlock.gif  }
InBlock.gif  
public string Time
InBlock.gif  {
InBlock.gif   
get{return DateTime.Now.ToString();}
InBlock.gif  }
InBlock.gif  
public string TestFun()
InBlock.gif  {
InBlock.gif   
return "Function of WebForm1 Called";
InBlock.gif  }
InBlock.gif  #region Web 窗体设计器生成的代码
InBlock.gif  override 
protected void OnInit(EventArgs e)
InBlock.gif  {
InBlock.gif   InitializeComponent();
InBlock.gif   base.OnInit(e);
InBlock.gif  }
InBlock.gif  
InBlock.gif  
private void InitializeComponent()
InBlock.gif  {    
InBlock.gif   this.Button1.Click 
+= new System.EventHandler(this.Button1_Click);
InBlock.gif   this.Load 
+= new System.EventHandler(this.Page_Load);
InBlock.gif
InBlock.gif  }
InBlock.gif  #endregion
InBlock.gif
InBlock.gif  
private void Button1_Click(object sender, System.EventArgs e)
InBlock.gif  {
InBlock.gif   Server.Transfer(
"WebForm2.aspx"true);
InBlock.gif  }
InBlock.gif
InBlock.gif
InBlock.gif在WebForm2上放置一个Literal1控件,程序如下:
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public Class WebForm2class WebForm2 : System.Web.UI.Page
InBlock.gif {
InBlock.gif  
protected System.Web.UI.WebControls.Literal Literal1;
InBlock.gif 
InBlock.gif  
private void Page_Load(object sender, System.EventArgs e)
InBlock.gif  {
InBlock.gif   
string strTxt="";
InBlock.gif   WebForm1 oForm
=(WebForm1)this.Context.Handler;
InBlock.gif   strTxt
+="Value of Textbox:"+Request.Form["TextBox1"+"<br/>";
InBlock.gif   strTxt
+="Time Property:"+oForm.Time +"<br/>";
InBlock.gif   strTxt
+="Context String:"+Context.Items["Context"].ToString() +"<br/>";
InBlock.gif   strTxt
+=oForm.TestFun() +"<br/>";
InBlock.gif   Literal1.Text 
=strTxt;
InBlock.gif  }
InBlock.gif
InBlock.gif  #region Web 窗体设计器生成的代码
InBlock.gif  override 
protected void OnInit(EventArgs e)
InBlock.gif  {
InBlock.gif   InitializeComponent();
InBlock.gif   base.OnInit(e);
InBlock.gif  }
InBlock.gif  
InBlock.gif  
private void InitializeComponent()
InBlock.gif  {    
InBlock.gif   this.Load 
+= new System.EventHandler(this.Page_Load);
InBlock.gif
InBlock.gif  }
InBlock.gif  #endregion
InBlock.gif }
InBlock.gif
InBlock.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值