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 Forms代码模型的详细信息,请参阅 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 主题和皮肤

适用于

另请参阅