我在网上看了一篇文章《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 属性值。
调用方法 | 属性值 |
---|---|
原始请求 |
|
回发 |
|
跨页发送 |
|
服务器传输 |
|
回调 |
|
注意 |
---|
确保对 PreviousPage 中引用的页的 IsCrossPagePostBack 属性进行测试。当前页的 IsCrossPagePostBack 属性始终返回 false |
试验代码
PageFrom的Html代码
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageFrom.aspx.cs" Inherits="PageFrom" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>PageFrom界面</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:TextBox ID="txtData" runat="server"></asp:TextBox>
- <asp:Button ID="btnClick" runat="server" Text="Button" PostBackUrl="~/PageTo.aspx" />
- </form>
- </body>
- </html>
PageFrom的CS代码
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class PageFrom : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (this.IsPostBack)
- {
- // The following code is only for test when click button
- this.Response.Write(this.TestData);
- }
- }
- public string TestData
- {
- get { return this.txtData.Text; }
- }
- }
PageTo的Html代码
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTo.aspx.cs" Inherits="PageTo" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>
PageTo的CS代码:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class PageTo : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (this.PreviousPage != null)
- {
- if (!this.IsCrossPagePostBack)
- {
- if (this.PreviousPage.IsValid)
- {
- /*** 试验位置 ***/
- this.Response.Write("Cross Page Post Back is ok....");
- }
- }
- }
- }
- }
其实我想在PageTo.cs中的/***试验位置***/那里增加一些代码,即把前一个页面转化成对应的类,然后通过这个类对象来访问相应的参数,而不是通过访问控件或者其他,最后的结果是在PageTo.cs中无法访问在PageFrom.cs中声明的类,有点让我很郁闷。
举一个非常简单的例子,我在PageFrom.cs中声明一个类,想在PageTo.cs中的PageTo类中引用那个类,就是不行。什么时候有时间的话,我得好好研究一下到底是怎么一回事。