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 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 网页验证用户输入是否不包含脚本或 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">
  
  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 主题和外观

适用于

另请参阅