共用方式為


Button.PostBackUrl 屬性

定義

取得或設定當按下 Button 控制項時,從目前的網頁要張貼到之目的地網頁的 URL。

public:
 virtual property System::String ^ PostBackUrl { System::String ^ get(); void set(System::String ^ value); };
[System.Web.UI.Themeable(false)]
public virtual string PostBackUrl { get; set; }
[<System.Web.UI.Themeable(false)>]
member this.PostBackUrl : string with get, set
Public Overridable Property PostBackUrl As String

屬性值

當按下 Button 控制項時,從目前的網頁要張貼到之目的地網頁的 URL。 預設值是空字串 (""),會使頁面回傳到本身。

實作

屬性

範例

下列程式代碼範例示範如何使用 PostBackUrl 屬性來執行跨頁面貼文。 當使用者按下控件時 Button ,頁面會將文字框中輸入的值張貼至 屬性所 PostBackUrl 指定的目標頁面。 若要執行此範例,您也必須在此程式代碼範例相同的目錄中建立目標頁面的檔案。 下一個範例會提供目標頁面的程序代碼。

<%@ page language="C#" %>

<!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 id="head1" runat="server">
  <title>Button.PostBackUrl Example</title>
</head>
<body>    
  <form id="form1" runat="server">
    
    <h3>Button.PostBackUrl Example</h3>

    Enter a value to post:
    <asp:textbox id="TextBox1" 
      runat="Server">
    </asp:textbox>

    <br /><br />

    <asp:button id="Button1" 
      text="Post back to this page"
      runat="Server">
    </asp:button>

    <br /><br />

    <asp:button id="Button2"
      text="Post value to another page" 
      postbackurl="Button.PostBackUrlPage2cs.aspx" 
      runat="Server">
    </asp:button>

  </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.PostBackUrl Example</title>
</head>
<body>    
  <form id="form1" runat="server">
    
    <h3>Button.PostBackUrl Example</h3>

    Enter a value to post:
    <asp:textbox id="TextBox1" 
      runat="Server">
    </asp:textbox>

    <br /><br />

    <asp:button id="Button1" 
      text="Post back to this page"
      runat="Server">
    </asp:button>

    <br /><br />

    <asp:button id="Button2"
      text="Post value to another page" 
      postbackurl="Button.PostBackUrlPage2vb.aspx" 
      runat="Server">
    </asp:button>

  </form>
</body>
</html>

下列程式代碼範例示範如何使用 Page.PreviousPage 屬性來存取使用 PostBackUrl 屬性從另一個頁面張貼的值。 此頁面會取得從上一頁張貼的字串,並將其顯示給使用者。 如果您嘗試直接執行此程式碼範例,將會收到錯誤,因為欄位的值 text 會是 null。 請改用此程式代碼來建立目標頁面,並將檔案放在與上一個範例的程式代碼相同的目錄中。 檔名必須對應至上一個範例中為 PostBackUrl 屬性指定的值。 當您執行上一個範例的程式代碼時,此頁面會在發生跨頁面貼文時自動執行。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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">
  
  void Page_Load (object sender, System.EventArgs e)
  {
    string text;
    
    // Get the value of TextBox1 from the page that 
    // posted to this page.
    text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;
    
    // Check for an empty string.
    if (text != "")
      PostedLabel.Text = "The string posted from the previous page is "
                         + text + ".";
    else
      PostedLabel.Text = "An empty string was posted from the previous page.";
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>Button.PostBackUrl Target Page Example</h3>
      
    <br />
    
    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

    </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">
  Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    Dim text As String
    
    ' Get the value of TextBox1 from the page that posted
    ' to this page.
    text = CType((PreviousPage.FindControl("TextBox1")), TextBox).Text
       
    ' Check for an empty string.
    If Not (text = "") Then
      PostedLabel.Text = "The string posted from the previous page is " _
                         & text & "."
    Else
      PostedLabel.Text = "An empty string was posted from the previous page."
    End If
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>Button.PostBackUrl Target Page Example</h3>
       
    <br />
    
    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

    </form>
</body>
</html>

備註

屬性 PostBackUrl 可讓您使用 Button 控件執行跨頁面文章。

注意

只有正確指定的路徑才能使用這個屬性。 例如,相對路徑 () Test/default.aspx 、絕對路徑 () https://localhost/WebApp/default.aspx 和虛擬 (~\Test\default.aspx) 正常運作。 格式不正確的路徑,例如 “/Test/default.aspx” 或 “\Test\default.aspx” 無法運作。 如需建立正確路徑的討論 ,請參閱 ASP.NET Web 項目 路徑。

PostBackUrl 屬性設定為單擊控件時要張貼至之網頁的 Button URL。 例如,指定 Page2.aspx 會導致包含 Button 控制件的頁面張貼至 Page2.aspx。 如果您未指定 屬性的值 PostBackUrl ,頁面會回傳給本身。

重要

使用伺服器端驗證的控制項執行跨頁面回傳時,您應該先檢查頁面 IsValid 的屬性是否 true 在處理回傳之前。 在跨頁面回傳的情況下,要檢查的頁面是 PreviousPage。 下列 VB 程序代碼示範如何完成此作業:

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.PreviousPage.IsValid Then
            ' Handle the post back
        Else
            Response.Write("Invalid")
        End If
End Sub

如需跨頁面張貼技術的詳細資訊,請參閱 ASP.NET Web Forms 中的跨頁面張貼

這個屬性無法由佈景主題或樣式表主題設定。 如需詳細資訊,請參閱 ThemeableAttributeASP.NET 主題和外觀

適用於

另請參閱