在網頁之間傳遞伺服器控制項值
建立 Web Form 應用程式時,從一個 Web Form 網頁傳遞資訊到另一個經常是必需的。這允許資訊在一個 Web Form 網頁輸入,並接著送出至另一個網頁來處理。
使用程式碼內嵌 (Inline) 在 Web Form 網頁之間傳遞數值的設計模式,與程式碼後置 (Code-Behind) 的檔案所使用的設計模式稍有差異。您要選擇哪一個設計模式,則依據您對使用內嵌程式碼或程式碼後置檔案的喜好。兩個設計模式都會在下列章節中作討論。
使用內嵌程式碼的設計模式
使用程式碼內嵌將值傳遞到另一個 Web Form 網頁時,您首先需要將類別名稱指定給含有您想要傳送的資訊的 Web Form 網頁。將 ClassName 屬性連同類別名稱包含於 @ Page 指示詞中,為 Web Form 網頁指定類別名稱。接下來,以類別中的 get 存取子為您想要共用的各個值建立屬性。get 存取子應該會傳回您想要傳遞的值,例如文字方塊的值。若要傳送資訊,請使用 Server 物件的 Transfer 方法,將應用程式的控制項傳輸到不同的 Web Form 網頁。
在接收的 Web Form 網頁上,透過在網頁頂端加入 @ Reference 指示詞,而將 Page 屬性設定為傳送的網頁,來參考宣告於傳送網頁中的類別。先擷取第一次從 Context 物件的 Handler 屬性收到 HTTP 要求的處理常式執行個體,然後接收的 Web Form 網頁就可以存取資訊。處理常式物件接著轉換為封裝傳遞的資訊的類別執行個體。一旦轉換執行之後,傳遞的值就可以透過已轉換物件的屬性來存取。
**警告 **請勿在網頁間傳遞機密的資訊 (例如信用卡資訊或密碼)。
若要建立將數值傳送到另一個 Web Form 網頁的 Web Form 網頁
在 Web Form 網頁頂端加入 @ Page 指示詞,而將 ClassName 屬性設定為有效類別名稱,來為 Web Form 網頁指定類別名稱。
<%@ Page Language="VB" ClassName="MyClassName" %> [C#] <%@ Page Language="C#" ClassName="MyClassName" %>
以類別中的 get 存取子為您想要傳遞給另一個 Web Form 網頁的各個值定義屬性。get 存取子應該只是傳回您想要傳遞的值,例如 Web Form 網頁上文字方塊的值。屬性必須定義於伺服器端指令碼。
<script runat="server"> Public ReadOnly Property FirstName() As String Get ' FirstNameTextBox is the name of a TextBox control. Return FirstNameTextBox.Text End Get End Property </script> [C#] <script runat="server"> public string FirstName { get { // FirstNameTextBox is the name of a TextBox control. return FirstNameTextBox.Text; } } </script>
當您想要傳遞資訊給另一個 Web Form 網頁時,例如按一下按鈕時,使用 HttpServerUtility.Transfer 方法來結束目前網頁的執行,並將應用程式的控制項傳輸到另一個 Web Form 網頁。HttpServerUtility.Transfer 方法接受單一參數,允許您指定想要傳輸控制項所至 Web Form 網頁的 URL。
Sub SubmitButtonClick(sender As Object, e As EventArgs) Server.Transfer("secondpage.aspx") End Sub [C#] void SubmitButtonClick(object sender, EventArgs e) { Server.Transfer("secondpage.aspx"); }
下列顯示如何建立 Web Form 網頁的完整範例,使用內嵌程式碼,將兩個 TextBox 控制項的值傳遞到另一個 Web Form 網頁。這個範例必須稱為 Firstpage.aspx。
<%@ Page Language="VB" ClassName="FirstPageClass" %>
<html>
<head>
<script runat="server">
Public ReadOnly Property FirstName() As String
Get
' first is the name of a TextBox control.
Return first.Text
End Get
End Property
Public ReadOnly Property LastName() As String
Get
' last is the name of a TextBox control.
Return last.Text
End Get
End Property
Sub ButtonClicked(sender As Object, e As EventArgs)
Server.Transfer("secondpage.aspx")
End Sub
</script>
</head>
<body>
<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" ClassName="FirstPageClass" %>
<html>
<head>
<script runat="server">
public string FirstName
{
get
{
return first.Text;
}
}
public string LastName
{
get
{
return last.Text;
}
}
void ButtonClicked(object sender, EventArgs e)
{
Server.Transfer("secondpage.aspx");
}
</script>
</head>
<body>
<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>
</body>
</html>
若要建立從另一個 Web Form 網頁接收數值的 Web Form 網頁
在接收資訊的 Web Form 網頁上,於網頁頂端加入 @ Reference 指示詞,並將 Page 屬性設定為來源 Web Form 網頁 (含有您想要傳遞的資訊之 Web Form 網頁)。
<%@ Reference Page="firstpage.aspx" %>
在伺服器端指令碼中宣告變數,以儲存定義於傳送資訊的 Web Form 網頁中的類別執行個體。
<script runat="server"> Dim fp As FirstPageClass </script> [C#] <script runat="server"> FirstPageClass fp; </script>
建立自訂 Page_Load 事件處理常式,將目前 HTTP 要求的 IHttpHandler 實作物件,在 Web Form 網頁沒有時自我公佈時,指派給上個步驟中的宣告變數。使用 IsPostBack 屬性決定網頁是否自我公佈。IHttpHandler 實作的物件包含初次收到 HTTP 要求之處理常式的執行個體。因為 IHttpHandler 實作的物件與先前步驟宣告的變數不是相同型別的物件,在它可以指派給變數之前,必須轉換為封裝從第一個 Web Form 網頁送來的資訊的類別。使用 HttpContext 物件的 Handler 屬性來擷取處理常式物件。
<script runat="server"> Sub Page_Load() If Not IsPostBack Then fp = CType(Context.Handler, FirstPageClass) End If End Sub </script> [C#] <script runat="server"> void Page_Load() { if (!IsPostBack) { fp = (FirstPageClass)Context.Handler; } } </script>
在第二步驟中宣告的變數現在包含封裝來自先前 Web Form 網頁的資訊的類別執行個體。使用變數存取含有從先前 Web Form 網頁送來的資訊類別的屬性。您可以利用程式設計方式存取這些值以執行計算,或只是使用指令碼分隔符號 <%= 和 %> 來顯示它們。
Hello <%=fp.FirstName%>
以下示範完整 Web Form 網頁,接收兩個來自另一個 Web Form 網頁的值。這些值接著顯示在 Web Form 網頁上。您必須將這個範例稱為 Secondpage.aspx。
<%@ Page Language="VB" %>
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
<script runat="server">
Dim fp As FirstPageClass
Sub Page_Load()
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub
</script>
</head>
<body>
<form runat="server">
Hello <%=fp.FirstName%> <%=fp.LastName%>
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" %>
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
<script runat="server">
FirstPageClass fp;
void Page_Load()
{
if (!IsPostBack)
{
fp = (FirstPageClass)Context.Handler;
}
}
</script>
</head>
<body>
<form runat="server">
Hello <%=fp.FirstName%> <%=fp.LastName%>
</form>
</body>
</html>
使用程式碼後置的檔案的設計模式
使用程式碼後置的檔案時,程式碼後置的檔案包含與 Web Form 網頁相關的程式碼之類別宣告。在傳送資訊的 Web Form 網頁的程式碼後置檔案中,先以類別中的 get 存取子為您想要共用的各個值建立屬性。get 存取子應該會傳回您想要傳遞的值,例如文字方塊的值。若要傳送資訊,請使用 Server 物件的 Transfer 方法,將應用程式的控制項傳輸到不同的 Web Form 網頁。
在接收的 Web Form 網頁上,透過在網頁頂端加入 @ Reference 指示詞,而將 Page 屬性設定為傳送的網頁,來參考宣告於傳送網頁中的類別。然後您可以擷取處理常式 (最先從 HttpContext 物件的 Handler 屬性收到 HTTP 要求) 的執行個體。
**注意 **若要讓宣告於傳送的 Web Form 網頁的類別在接收 Web Form 網頁的程式碼後置檔案中可供使用,您必須使用命令列編譯器將每一個 Web Form 網頁的程式碼後置檔案手動編譯成單一 .dll 檔案。.dll 檔案必須置於 Web Form 應用程式的 \Bin 目錄中。
處理常式物件接著轉換為封裝傳遞的資訊的類別執行個體。一旦轉換執行之後,傳遞的值就可以透過已轉換物件的屬性來存取。
**警告 **請勿在網頁間傳遞機密的資訊 (例如信用卡資訊或密碼)。
若要從不同 Web Form 網頁傳送伺服器控制項值
為傳送的 Web Form 網頁建立程式碼後置的檔案,包含與網頁相關程式碼的類別宣告。
Imports System ' Add other references here. Public Class FirstPageClass Inherits System.Web.UI.Page ' Add class code here. End Class [C#] Imports System // Add other references here. public class FirstPageClass : System.Web.UI.Page { // Add class code here. }
若要在宣告於程式碼後置檔案的類別中存取 Web Form 網頁上的伺服器控制項,請在類別中宣告保護 (Protected) 變數來表示您想要存取的伺服器控制項。
Protected FirstNameTextBox As System.Web.UI.WebControls.TextBox [C#] protected System.Web.UI.WebControls.TextBox FirstNameTextBox;
在宣告於第一步驟的類別中,以 get 存取子為您想要傳遞給另一個 Web Form 網頁的各個值定義屬性。get 存取子應該只是傳回您想要傳遞的值,例如 Web Form 網頁上文字方塊的值。
Public ReadOnly Property FirstName() As String Get ' FirstNameTextBox is the name of a TextBox control. Return FirstNameTextBox.Text End Get End Property [C#] public string FirstName { get { // FirstNameTextBox is the name of a TextBox control. return FirstNameTextBox.Text; } }
當您想要傳遞資訊給不同 Web Form 網頁時,例如按一下按鈕時,使用 HttpServerUtility.Transfer 方法來結束目前網頁的執行,並將應用程式的控制項傳輸到另一個 Web Form 網頁。HttpServerUtility.Transfer 方法接受單一參數,允許您指定想要傳輸控制項所至 Web Form 網頁的 URL。
Sub SubmitButtonClick(sender As Object, e As EventArgs) Server.Transfer("secondpage.aspx") End Sub [C#] void SubmitButtonClick(object sender, EventArgs e) { Server.Transfer("secondpage.aspx"); }
為傳送資訊的 Web Form 網頁建立介面。務必將 Inherits 屬性加入至 @ Page 指示詞,這屬性要設定為程式碼後置檔案中宣告的類別。
<%@ Page Language="VB" Inherits="FirstPageClass" %> <html> <head> </head> <body> <!-- Add User Interface here --> </body> [C#] <%@ Page Language="C#" Inherits="FirstPageClass" %> <html> <head> </head> <body> <!-- Add User Interface here --> </body>
以下示範與傳送資訊的 Web Form 網頁相關的程式碼後置檔案的完整範例。根據您使用的是 Visual Basic 還是 C#,確定將這些範例分別稱為 Firstpage.aspx.vb 或 Firstpage.aspx.cs。
Imports System
Public Class FirstPageClass :
Inherits System.Web.UI.Page
Protected first As System.Web.UI.WebControls.TextBox
Protected last As System.Web.UI.WebControls.TextBox
Protected Button1 As System.Web.UI.WebControls.Button
Public ReadOnly Property FirstName() As String
Get
' first is the name of a TextBox control.
Return first.Text
End Get
End Property
Public ReadOnly Property LastName() As String
Get
' last is the name of a TextBox control.
Return last.Text
End Get
End Property
Sub ButtonClicked(sender As Object, e As EventArgs)
Server.Transfer("secondpage.aspx")
End Sub
End Class
[C#]
using System;
public class FirstPageClass : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox first;
protected System.Web.UI.WebControls.TextBox last;
protected System.Web.UI.WebControls.Button Button1;
public string FirstName
{
get
{
return first.Text;
}
}
public string LastName
{
get
{
return last.Text;
}
}
void ButtonClicked(object sender, EventArgs e)
{
Server.Transfer("secondpage.aspx");
}
}
下列顯示如何建立 Web Form 網頁的完整範例,使用程式碼後置檔案,將兩個 TextBox 控制項的值傳遞到另一個 Web Form 網頁。請確定這個範例稱為 Firstpage.aspx。
<%@ Page Language="VB" Inherits="FirstPageClass" %>
<html>
<head>
</head>
<body>
<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
id="Button1"
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" Inherits="FirstPageClass" %>
<html>
<head>
</head>
<body>
<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
id="Button1"
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>
</body>
</html>
若要從不同 Web Form 網頁接收伺服器控制項值
為接收的 Web Form 網頁建立程式碼後置的檔案,包含與網頁相關程式碼的類別宣告。
Imports System ' Add other references here. Public Class SecondPageClass Inherits System.Web.UI.Page ' Add class code here. End Class [C#] Imports System // Add other references here. public class SecondPageClass : System.Web.UI.Page { // Add class code here. }
若要在程式碼後置檔案中存取 Web Form 網頁上的伺服器控制項,請在類別中宣告保護變數來表示您想要存取的伺服器控制項。
Protected FirstNameTextBox As System.Web.UI.WebControls.TextBox [C#] protected System.Web.UI.WebControls.TextBox FirstNameTextBox;
在類別中宣告變數以儲存定義於傳送資訊的 Web Form 網頁中的類別執行個體。如果您想要變數在接收的 Web Form 網頁中可供使用,就令它為公用的 (Public)。
**注意 **無法存取定義於傳送資訊的 Web Form 網頁的類別,直到您將各個 Web Form 網頁的程式碼後置檔案編譯成單一 .dll 檔案。.dll 檔案必須置於 Web 應用程式的 \Bin 目錄中。這個過程會在稍後的步驟中描述。
Public fp As FirstPageClass [C#] public FirstPageClass fp;
建立自訂 Page_Load 事件處理常式,將目前 HTTP 要求的 IHttpHandler 實作物件,在 Web Form 網頁沒有時自我公佈時,指派給上個步驟中的宣告變數。使用 IsPostBack 屬性決定網頁是否自我公佈。IHttpHandler 實作的物件包含初次收到 HTTP 要求之處理常式的執行個體。因為 IHttpHandler 實作的物件與先前步驟宣告的變數不是相同型別的物件,在它可以指派給變數之前,必須轉換為封裝從第一個 Web Form 網頁送來的資訊的類別。使用 HttpContext 物件的 Handler 屬性來擷取處理常式物件。
Sub Page_Load() If Not IsPostBack Then fp = CType(Context.Handler, FirstPageClass) End If End Sub [C#] void Page_Load() { if (!IsPostBack) { fp = (FirstPageClass)Context.Handler; } }
在第三步驟中宣告的變數現在包含封裝來自先前 Web Form 網頁的資訊的類別執行個體。使用變數存取從先前 Web Form 網頁送來的資訊。您可以利用程式設計方式存取這些值以執行計算,或只是使用指令碼分隔符號 <%= 和 %> 來顯示它們。
Hello <%=fp.FirstName%>
為傳送資訊的 Web Form 網頁建立介面。在 @ Page 指示詞中,務必加入 Inherits 屬性,這屬性要設定為程式碼後置的檔案中宣告的類別。
<%@ Page Language="VB" Inherits="SecondPageClass" %> <html> <head> </head> <body> <!-- Add User Interface here --> </body> [C#] <%@ Page Language="C#" Inherits="SecondPageClass" %> <html> <head> </head> <body> <!-- Add User Interface here --> </body>
在接收資訊的 Web Form 網頁頂端加入 @ Reference 指示詞,令 Page 屬性設定為來源 Web Form 網頁 (含有您想要傳遞的資訊的 Web Form 網頁)。
<%@ Reference Page="firstpage.aspx" %>
傳送和接收 Web Form 網頁都在這個時刻完成。然而,在應用程式可以正確執行之前,各個網頁的程式碼後置檔案必須一起編譯成單一的 .dll 檔案。此 .dll 檔案必須置於 Web 應用程式的 \Bin 目錄中。這可以讓宣告於傳送的 Web Form 網頁中的類別可以在接收的 Web Form 網頁中存取。若要編譯程式碼後置的檔案,請在命令列輸入下列命令:
vbc /out:Bin\appname.dll /r:System.dll /r:System.Web.dll /t:library firstpage.aspx.vb secondpage.aspx.vb [C#] csc /out:Bin\appname.dll /r:System.dll /r:System.Web.dll /t:library firstpage.aspx.cs secondpage.aspx.cs
以下示範與接收資訊的 Web Form 網頁相關的程式碼後置檔案的完整範例。根據您使用的是 Visual Basic 還是 C#,確定將這些範例分別稱為 Secondpage.aspx.vb 或 Secondpage.aspx.cs。
Imports System
Public Class SecondPageClass
Inherits System.Web.UI.Page
Protected DisplayLabel As System.Web.UI.WebControls.Label
Public fp As FirstPageClass
Sub Page_Load()
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub
End Class
[C#]
using System;
public class SecondPageClass : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label DisplayLabel;
public FirstPageClass fp;
void Page_Load()
{
if (!IsPostBack)
{
fp = (FirstPageClass) Context.Handler;
}
}
}
以下示範如何建立 Web Form 網頁的完整範例,使用程式碼後置的檔案來接收自不同 Web Form 網頁傳來的值。請確定這個範例稱為 Secondpage.aspx
<%@ Page Language="VB" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
</head>
<body>
<form runat="server">
Hello <%=fp.FirstName%> <%=fp.LastName%>
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
</head>
<body>
<form runat="server">
Hello <%=fp.FirstName%> <%=fp.LastName%>
</form>
</body>
</html>
請參閱
@Page 指示詞 | HttpServerUtility.Transfer | TextBox | IHttpHandler | Handler