学习ASP.NET 2.0中跨页提交

来源:互联网 发布:2016如家酒店数据分析 编辑:程序博客网 时间:2024/06/05 14:44

我在网上看了一篇文章《ASP.NET 2.0功能扩展:跨页提交》,由于文章中使用的是VB.NET代码,所以我就在C#上做了试验,发现了一些问题,并且查了相应的资料。整理如下:

 

URL: http://www.webjx.com/aspnet/2007-12-07/1839.html

 

MSDN上的资料:

 

通常有必要了解调用 ASP.NET 网页的方式:是由原始请求 (HTTP GET)、回发 (HTTP POST)、来自其他页的跨页发送 (HTTP POST) 调用的,还是由来自其他页(使用 Transfer 方法或使用浏览器中的回调)的传送调用的。Page 类公开可用于确定页调用方式的属性集。

 

下表列出了页调用方式及对应的 Page 属性值。

调用方法 属性值

原始请求

  • IsPostBack 设置为 false

  • PreviousPage 设置为 null(在 Visual Basic 中为 Nothing)。

  • IsCallback 设置为 false

回发

  • IsPostBack 设置为 true

  • PreviousPage 设置为 null(在 Visual Basic 中为 Nothing)。

  • IsCallback 设置为 false

跨页发送

  • IsPostBack 设置为 false

  • PreviousPage 引用源页。

  • IsCrossPagePostBack 设置为 true

  • IsCallback 设置为 false

服务器传输

  • IsPostBack 设置为 false

  • PreviousPage 引用源页。

  • PreviousPage 中引用的 IsCrossPagePostBack 设置为 false

  • IsCallback 设置为 false

回调

  • IsPostBack 设置为 false

  • PreviousPage 设置为 null(在 Visual Basic 中为 Nothing)。

  • IsCallback 设置为 true

注意

确保对 PreviousPage 中引用的页的 IsCrossPagePostBack 属性进行测试。当前页的 IsCrossPagePostBack 属性始终返回 false

 

试验代码

 

PageFrom的Html代码

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageFrom.aspx.cs" Inherits="PageFrom" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>PageFrom界面</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <asp:TextBox ID="txtData" runat="server"></asp:TextBox>
  10.         <asp:Button ID="btnClick" runat="server" Text="Button" PostBackUrl="~/PageTo.aspx" />
  11.     </form>
  12. </body>
  13. </html>

PageFrom的CS代码

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class PageFrom : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         if (this.IsPostBack)
  16.         {
  17.             // The following code is only for test when click button
  18.             this.Response.Write(this.TestData);
  19.         }
  20.     }
  21.     public string TestData
  22.     {
  23.         get { return this.txtData.Text; }
  24.     }
  25. }

 

PageTo的Html代码

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTo.aspx.cs" Inherits="PageTo" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>无标题页</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         
  11.     </div>
  12.     </form>
  13. </body>
  14. </html>

 

PageTo的CS代码:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class PageTo : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         if (this.PreviousPage != null)
  16.         {
  17.             if (!this.IsCrossPagePostBack)
  18.             {
  19.                 if (this.PreviousPage.IsValid)
  20.                 {
  21.                     /***   试验位置 ***/
  22.                     this.Response.Write("Cross Page Post Back is ok....");
  23.                 }
  24.             }
  25.         }
  26.     }
  27. }

其实我想在PageTo.cs中的/***试验位置***/那里增加一些代码,即把前一个页面转化成对应的类,然后通过这个类对象来访问相应的参数,而不是通过访问控件或者其他,最后的结果是在PageTo.cs中无法访问在PageFrom.cs中声明的类,有点让我很郁闷。

 

举一个非常简单的例子,我在PageFrom.cs中声明一个类,想在PageTo.cs中的PageTo类中引用那个类,就是不行。什么时候有时间的话,我得好好研究一下到底是怎么一回事。

原创粉丝点击