LinkButton.PostBackUrl 屬性

定義

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

屬性值

String

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

實作

屬性

範例

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

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

    <br /><br />

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

    <br /><br />

    <asp:linkbutton id="LinkButton2"
      text="Post value to another page" 
      postbackurl="LinkButton.PostBackUrlPage2cs.aspx" 
      runat="Server">
    </asp:linkbutton>

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

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

    <br /><br />

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

    <br /><br />

    <asp:linkbutton id="LinkButton2"
      text="Post value to another page" 
      postbackurl="LinkButton.PostBackUrlPage2vb.aspx" 
      runat="Server">
    </asp:linkbutton>

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

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

備註

屬性 PostBackUrl 可讓您使用 LinkButton 控制項執行跨頁面文章。 將 PostBackUrl 屬性設定為按一下控制項時要張貼至之網頁的 LinkButton URL。 例如,指定 Page2.aspx 會導致包含 LinkButton 控制項的頁面張貼至 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 主題和外觀

適用於

另請參閱