Page.PreviousPage Page.PreviousPage Page.PreviousPage Page.PreviousPage Property

定義

取得將控制權移轉給目前頁面的頁面。Gets the page that transferred control to the current page.

public:
 property System::Web::UI::Page ^ PreviousPage { System::Web::UI::Page ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.Page PreviousPage { get; }
member this.PreviousPage : System.Web.UI.Page
Public ReadOnly Property PreviousPage As Page

屬性值

Page,表示將控制權移轉給目前頁面的頁面。The Page representing the page that transferred control to the current page.

例外狀況

不允許目前的使用者存取上一頁。The current user is not allowed to access the previous page.

-或--or- ASP.NET 路由正在使用中,先前頁面的 URL 是路由的 URL。ASP.NET routing is in use and the previous page's URL is a routed URL. 當 ASP.NET 檢查存取權限時,會假設 URL 是檔案的實際路徑。When ASP.NET checks access permissions, it assumes that the URL is an actual path to a file. 因為這不是使用路由 URL 的情況,檢查便會失敗。Because this is not the case with a routed URL, the check fails.

範例

下列範例是在兩個部分。The following example is in two parts. 第一種是使用 ASP.NET 網頁Transfer方法,做為頁面模型中公開Server.Transfer("path")The first is an ASP.NET page that uses the Transfer method, exposed in the page model as Server.Transfer("path"). 第二個部分是 [目標] 頁面,使用PreviousPage屬性來取得第一頁的標題。The second part is the target page, which uses the PreviousPage property to get the title of the first page.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
    protected void Page_Load(object sender, EventArgs e)
    {
        // If second is an even number, the server is available
        // Replace this line with a valid check for the server.
        bool IsServerAvailable = (DateTime.Now.Second % 2 == 0);
        
        if (!IsServerAvailable)
            Server.Transfer("Notify.aspx", true);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Switch Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Available</h2>
    
    <p>This page appears if the database server 
        is available.</p>
    
    <p>Enter a pretend Server Name: 
        <asp:TextBox ID="serverNameText" 
        runat="server">MyDatabaseServer</asp:TextBox>
    </p>
    
    <p><asp:Button ID="SubmitButton" runat="server" 
        Text="Is server available?" /></p>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As EventArgs)

        Dim IsServerAvailable As Boolean

        ' If second is an even number, the server is available
        ' Replace this line with a valid check for the server.
        IsServerAvailable = (DateTime.Now.Second Mod 2 = 0)

        If Not IsServerAvailable Then
            Server.Transfer("Notify.aspx", True)
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Switch Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Available</h2>
    
    <p>This page appears if the database server 
        is available.</p>
    
    <p>Enter a pretend Server Name: 
        <asp:TextBox ID="serverNameText" 
        runat="server">MyDatabaseServer</asp:TextBox>
    </p>
    
    <p><asp:Button ID="SubmitButton" runat="server" 
        Text="Is server available?" /></p>
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
    protected void Page_Load(object sender, EventArgs e)
    {
        // Find the server name on the previous page
        TextBox txt = 
                (TextBox)Page.PreviousPage.FindControl("serverNameText");
        if (txt != null)
            prevServerName.Text = Server.HtmlEncode(txt.Text);
        else
            prevServerName.Text = "[Name Not available]";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Not Available</h2>

    <p>This page appears if the named database server is not 
    available, but the URL displays as the main target page.</p>
    
    <p>Server Name (From Page.PreviousPage): 
        <asp:Label ID="prevServerName" runat="server" /></p>
    
    <p>Refresh the page to see if the server is now available.</p>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As EventArgs)

        Dim txt As TextBox

        ' Find the server name on the previous page
        txt = CType(Page.PreviousPage.FindControl _
            ("serverNameText"), TextBox)
        If Not IsNothing(txt) Then
            prevServerName.Text = Server.HtmlEncode(txt.Text)
        Else
            prevServerName.Text = "[Name Not available]"
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Not Available</h2>

    <p>This page appears if the named database server is not 
    available, but the URL displays as the main target page.</p>
    
    <p>Server Name (From Page.PreviousPage): 
        <asp:Label ID="prevServerName" runat="server" /></p>
    
    <p>Refresh the page to see if the server is now available.</p>
    </div>
    </form>
</body>
</html>

備註

當您使用Transfer原始頁面方法或使用跨網頁公佈到從一個 ASP.NET 網頁的處理傳輸到另一個,包含可能需要的 [目的地] 頁面的要求資訊。When you use the Transfer method or use cross-page posting to transfer processing from one ASP.NET page to another, the originating page contains request information that might be required for the destination page. 您可以使用PreviousPage屬性來存取該資訊。You can use the PreviousPage property to access that information.

如果目前的頁面正在直接要求 (不轉移或從另一頁的轉錄文章) 的結果呈現PreviousPage屬性包含nullIf the current page is being rendered as a result of a direct request (not a transfer or cross-post from another page), the PreviousPage property contains null.

適用於