選擇具有PostBackUrl屬性的三個控件Button、LinkButton、ImageButton。PostBackUrl屬性的值就是投遞的頁面URL。
要在接收頁面按對象的方式接收投遞頁面的表單中的值有兩種方式:
1、PreviousPage.FindControl(“控件變量名”),這個方法返回的是Control類型的變量(對象),假設這個控件對象是TextBox只要強制轉換就可以訪問它的屬性
Demo:
打開vs2005/vs2008新建->網站->ASP.NET,創建后在默認的Default.aspx中設計如圖:
打開源:
?
<%@ Page Language="C#" AutoEventWireup="true"? CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
??????? <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
??????? <asp:Button ID="btnSubmit"? runat="server" Text="傳遞數據" />
??? </div>
??? </form>
</body>
</html>
?
新建web窗體NextPage.aspx
?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NexPage.aspx.cs" Inherits="NexPage" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
<!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>
?
打開NextPage.aspx.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 NexPage : System.Web.UI.Page
{
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? Control control = (TextBox)PreviousPage.FindControl("txtUserName");
??????? string conent = control.Text;
??? }
}
?
?
2、在原始頁面(投遞頁)中加入要在被投遞頁訪問的表單元素對象的相應屬性,就可以在被投遞頁自如的訪問了,這看起來很奇怪,為什么你會感覺奇怪呢?這是因為你很容易忘記頁面是一個類,而你要訪問的控件只不過是類中的一個私有字段,給一個類中的私有字段加一個公開的屬性是再自然不過的事情。
Demo:
打開vs2005/vs2008新建->網站->ASP.NET,創建后在默認的Default.aspx中設計如圖:
這是查看源:
<%@ Page Language="C#" AutoEventWireup="true"? CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
??????? <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
??????? <asp:Button ID="btnSubmit" PostBackUrl="~/NextPage.aspx" runat="server" Text="傳遞數據" />
??? </div>
??? </form>
</body>
</html>
?
然后再打開Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{?
//屬性
??? public TextBox TxtUserName
??? {
??????? get { return this.txtUserName; }
??? }
??? protected void Page_Load(object sender, EventArgs e)
??? {
???????
??? }
}
?
?在資源管理器中添加新項中添加web窗體NextPage.aspx添加PreviousPageType 指令
?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NextPage.aspx.cs" Inherits="NextPage" %>
<%@ PreviousPageType VirtualPath = "~/Default.aspx" %>
<!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>
打開NextPage.aspx.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 NextPage : System.Web.UI.Page
{
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? if (this.PreviousPage != null)
??????? {//判斷PreviousPage是否存在
??????????? if (this.PreviousPage.IsCrossPagePostBack)
??????????? {//判斷頁面是否跨頁傳遞
??????????????? string conent = PreviousPage.TxtUserName.Text;//獲取值
??????????????? //將值輸出
??????????????? Response.Write(conent);
??????????? }
??????? }
???????
??? }
}
?
處理跨頁投遞的代碼面臨著一種風險,如果請求不是由投遞頁發送過來的,那么代碼訪問的對象并不純在,這將產生一個異常,使用IsCrossPagePostBack屬性可以區別上述情形,此屬性在投遞頁面屬于Page對象Page.IsCrossPagePostBack(本頁是否參與到跨頁投遞),在被投遞頁面訪問時是這樣的:PreviousPage.IsCrossPagePostBack(請求是否是來自投遞頁)
還要注意的一點是要檢查一下PreviousPage是否存在this.PreviousPage? = = null