ImageButton.PostBackUrl 屬性

定義

取得或設定當按下 ImageButton 控制項時,從目前的網頁要張貼到之目的地網頁的 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

屬性值

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

實作

屬性

範例

下列程式碼範例示範如何使用 PostBackUrl 屬性來執行跨頁面文章。 當使用者按一下 ImageButton 控制項時,頁面會將文字方塊中輸入的值張貼至 屬性所 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>ImageButton.PostBackUrl Example</title>
</head>
<body>    
  <form id="form1" runat="server">
    
    <h3>ImageButton.PostBackUrl Example</h3>

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

    <br /><br />

    <asp:imagebutton id="ImageButton1"
      imageUrl=""
      alternatetext="Post back to this page"
      runat="Server">
    </asp:imagebutton>

    <br /><br />

    <asp:imagebutton id="ImageButton2"
      imageUrl=""
      alternatetext="Post value to another page" 
      postbackurl="ImageButton.PostBackUrlPage2cs.aspx" 
      runat="Server">
    </asp:imagebutton>

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

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

    <br /><br />

    <asp:imagebutton id="PostImageButton"
      imageUrl="Images\PostButton.jpg"
      alternatetext="Post back to this page"
      runat="Server">
    </asp:imagebutton>

    <br /><br />

    <asp:imagebutton id="CrossPostImageButton"
      imageUrl="Images\CrossPostButton.jpg"
      alternatetext="Post value to another page" 
      postbackurl="ImageButton.PostBackUrlPage2vb.aspx" 
      runat="Server">
    </asp:imagebutton>

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

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

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型

<%@ 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>ImageButton.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>ImageButton.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>ImageButton.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>ImageButton.PostBackUrl Target Page Example</h3>
       
    <br />
    
    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

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

備註

屬性 PostBackUrl 可讓您使用 ImageButton 控制項執行跨頁面文章。 將 PostBackUrl 屬性設定為按一下控制項時 ImageButton 要張貼到之網頁的 URL。 例如,指定 Page2.aspx 會導致包含 ImageButton 控制項的頁面張貼至 Page2.aspx 。 如果您未指定 屬性的值 PostBackUrl ,頁面會回傳給本身。

重要

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

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 主題和麵板

適用於

另請參閱