.net页面间传值的几种经常使用方法

1。使用QueryString
      使用QuerySting在页面间传递值已是一种很老的机制了,这种方法的主要优势是实现起来很是简单,然而它的缺点是传递的值是会显示在浏览器的地址栏上的(不安全),同时又不能传递对象,可是在传递的值少而安全性要求不高的状况下,这个方法仍是一个不错的方案。使用这种方法的步骤以下:
1,使用控件建立web表单(form)
2,建立能够返回表单的按钮和连接按钮
3,在按钮或连接按钮的单击事件里建立一个保存URL的字符变量
4,在保存的URL里添加QueryString参数
5,使用Response.Redirect重定向到上面保存的URL
下面的代码片段演示了如何实现这个方法:
  源页面WebForm1.aspx.cs中的部分代码:web

  
  
  
  
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.      string url;  
  4.      url="WebForm2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;  
  5.      Response.Redirect(url);  
  6. }  
  7.  目标页面WebForm2.aspx.cs中的部分代码:  
  8. private void Page_Load(object sender, System.EventArgs e)  
  9. {  
  10.      Label1.Text=Request.QueryString["name"];  
  11.      Label2.Text=Request.QueryString["email"];  


2。使用Session变量
      使用Session变量是能够在页面间传递值的的另外一种方式,在本例中咱们把控件中的值存在Session变量中,而后在另外一个页面中使用它,以不一样页面间实现值传递的目的。可是,须要注意的是在Session变量存储过多的数据会消耗比较多的服务器资源,在使用session时应该慎重,固然了,咱们也应该使用一些清理动做来去除一些不须要的session来下降资源的无谓消耗。使用Session变量传递值的通常步骤以下:
1,在页面里添加必要的控件
2,建立能够返回表单的按钮和连接按钮
3,在按钮或连接按钮的单击事件里,把控件的值添加到session变量里
4,使用Response.Redirect(或Server.Transfer)方法重定向到另外一个页面
5,在另外一个页面提取session的值,在肯定不须要使用该session时,要显式清除它
下面的代码片段演示了如何实现这个方法:
   源页面WebForm1.aspx.cs中的部分代码:浏览器

  
  
  
  
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.      //textbox1 and textbox2 are webform  
  4.      //controls  
  5.      Session["name"]=TextBox1.Text;  
  6.      Session["email"]=TextBox2.Text;  
  7.      Server.Transfer("WebForm2.aspx");  
  8. }  
  9.   目标页面WebForm2.aspx.cs中的部分代码:  
  10. private void Page_Load(object sender, System.EventArgs e)  
  11. {  
  12.      Label1.Text=Session["name"].ToString();  
  13.      Label2.Text=Session["email"].ToString();  
  14.      Session.Remove("name");  
  15.      Session.Remove("email");  
  16. }  

3.使用Server.Transfer
      这个方法相比上面介绍的方法稍微复杂一点,但在页面间值传递中倒是特别有用的,使用该方法你能够在另外一个页面以对象属性的方式来存取显露的值,固然了,使用这种方法,你须要额外写一些代码以建立一些属性以即可以在另外一个页面访问它,可是,这个方式带来的好处也是显而易见的。整体来讲,使用这种方法是简洁的同时又是面向对象的。使用这种方法的整个过程以下:
1,在页面里添加必要的控件
2,建立返回值的Get属性过程
3,建立能够返回表单的按钮和连接按钮
4,在按钮单击事件处理程序中调用Server.Transfer方法转移到指定的页面
5,在第二个页面中,咱们就可使用Context.Handler属性来得到前一个页面实例对象的引用,经过它,就可使用存取前一个页面的控件的值了
如下代码综合实现上述步骤过程的代码:
  源页面WebForm1.aspx.cs中的部分代码:
    把如下的代码添加到页面中
 安全

  
  
  
  
  1. public string Name  
  2. {  
  3.      get  
  4.      {  
  5.          return TextBox1.Text;  
  6.      }  
  7. }  
  8.  
  9. public string EMail  
  10. {  
  11.      get  
  12.      {  
  13.          return TextBox2.Text;  
  14.      }  
  15. }  
  16.   而后调用Server.Transfer方法  
  17. private void Button1_Click(object sender, System.EventArgs e)  
  18. {  
  19.      Server.Transfer("WebForm2.aspx");  
  20. }  
  21.    目标页面代码:  
  22.  
  23. 在WebForm2.aspx中务必在第一句话添加  
  24.  
  25. <%@ Reference Page="~/WebForm1.aspx" %>或  
  26.  
  27. <%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %> 
  28.  
  29. 而后在WebForm2.aspx.cs中添加以下。  
  30.  
  31.  
  32. private void Page_Load(object sender, System.EventArgs e)  
  33. {  
  34.      //create instance of source web form  
  35.      WebForm1 wf1;  
  36.      //get reference to current handler instance  
  37.      wf1=(WebForm1)Context.Handler;  
  38.      Label1.Text=wf1.Name;  
  39.      Label2.Text=wf1.EMail;  
  40. }  
  41.  

若是在调试的过程当中遇到错误.就到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files中把新建的网站名的文件夹删掉.(要先关掉Visual Studio开发环境再删)服务器

4.使用@PreviousPageType指令cookie

      这个指令是.net 2.0中的一个新指令,用于处理ASP.NET 2.0提供的跨页面传送新功能.用于批定跨页面的传送过程起始于哪一个页面.包含两个属性:session

TypeName:设置回送时的派生类名ide

VirtualPath:设置回送时所传送页面的地址.网站

以下示例:this

  
  
  
  
  1. 源页面WebForm1.aspx中有一个TextBox,ID为txtName.在WebForm1.aspx.cs中设置一个属性:  
  2.  
  3. public TextBox Name  
  4.  
  5. {  
  6.  
  7.     get{return this.txtName;}//返回一个控件对象  
  8.  
  9. }  
  10.  
  11. 在目标页面的设计文件中(WebForm2.aspx)的最上方加上:  
  12.  
  13.  <%@ PreviousPageType VirtualPath="~/Page1.aspx"%>,  
  14.  
  15. 而后就能引用WebForm1.aspx中定义的属性了.  
  16.  
  17. 在WebForm2.aspx.cs中能够有以下引用形式(假设WebForm2.aspx中有一个ID为lblName的Label):  
  18.  
  19. lblName.Text="Hello"+PreviousPage.Name.Text+"<br />";  
  20.  

 5.利用某些控件的PostBackUrl属性url

示例:仍然是源页面WebForm1.aspx和目标页面WebForm2.aspx.

WebForm1.aspx中的部分代码:

 

  
  
  
  
  1. <asp:Button ID="btnPostBack" Runat="server" Text="PBButton"></asp:Button> 
  2.  
  3. <asp:TextBox ID="txtName" Runat="server" ></asp:TextBox> 
  4.  
  5. <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar> 
  6.  
  7. WebForm2.aspx.cs中的部分代码:  
  8.  
  9. protected void Page_Load(object Sender,System.EventArgs e)  
  10.  
  11. {  
  12.  
  13.     TextBox txtName;  
  14.  
  15.     Calendar calendar1;  
  16.  
  17.     txtName=(TextBox)PreviousPage.FindControl("txtName");  
  18.  
  19.     calendar1=(Calendar)PreviousPage.FindControl("Calendar1");  
  20.  
  21.     Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();  
  22.  
  23. }  
  24.  

使用这种方法存在一个问题:若是在没有单击那个按钮以前,也就是未处理WebForm1.aspx以前,有人请求了WebForm2.aspx,该怎么办?这就须要在WebForm2.aspx中的代码处理以前加一个判断.使用IsCrossPagePostBack属性,这与IsPostBack属性很类似,它容许检查请求是否来自WebForm1.aspx.以下:

  
  
  
  
  1. protected void Page_Load(object Sender,System.EventArgs e)  
  2.  
  3. {  
  4.  
  5.     if(PreviousPage.IsCrossPagePostBack)  
  6.  
  7.     {  
  8.  
  9.         TextBox txtName;  
  10.  
  11.         Calendar calendar1;  
  12.  
  13.         txtName=(TextBox)PreviousPage.FindControl("txtName");  
  14.  
  15.         calendar1=(Calendar)PreviousPage.FindControl("Calendar1");  
  16.  
  17.         Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();  
  18.  
  19.     }  
  20.  
  21.     else  
  22.  
  23.     {  
  24.  
  25.         Response.Redirect("WebForm1.aspx");  
  26.  
  27.     }  
  28.  
  29. }  
  30.  

6.  使用Cookie对象变量
  这个也是你们常使用的方法,与Session同样,是对每个用户而言的,可是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。并且Cookie的使用要配合ASP.NET内置对象Request来使用。

a.aspx的C#代码
 

  
  
  
  
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.     HttpCookie cookie_name = new HttpCookie("name");  
  4.     cookie_name.Value = Label1.Text;  
  5.     Reponse.AppendCookie(cookie_name);  
  6.     Server.Transfer("b.aspx");  
  7. }  
  8.  
  9. b.aspx中C#代码  
  10. private void Page_Load(object sender, EventArgs e)  
  11. {  
  12.     string name;  
  13.     name = Request.Cookie["name"].Value.ToString();  
  14. }  
  15.  

7.  使用Application 对象变量
  Application对象的做用范围是整个全局,也就是说对全部用户都有效。其经常使用的方法用Lock和UnLock。
a.aspx的C#代码
 

  
  
  
  
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.     Application["name"] = Label1.Text;  
  4.     Server.Transfer("b.aspx");  
  5. }  
  6.  
  7. b.aspx中C#代码  
  8. private void Page_Load(object sender, EventArgs e)  
  9. {  
  10.     string name;  
  11.     Application.Lock();  
  12.     name = Application["name"].ToString();  
  13.     Application.UnLock();  
  14. }  
  15.  

 

 转载出处:http://blog.csdn.net/MicrosoftCenterOfHN/archive/2009/05/12/4170404.aspx

相关文章
相关标签/搜索