Button.PostBackUrl 属性
定义
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。The URL of the Web page to post to from the current page when the Button control is clicked. 默认值为空字符串 (""),表示将页回发到自身。The default value is an empty string (""), which causes the page to post back to itself.
实现
- 属性
示例
下面的代码示例演示如何使用 PostBackUrl 属性来执行跨页面的 post。The following code example demonstrates how to use the PostBackUrl property to perform a cross-page post. 当用户单击控件时 Button ,该页将在文本框中输入的值发送到属性指定的目标页 PostBackUrl 。When the user clicks the Button control, the page posts the value entered in the text box to the target page specified by the PostBackUrl property. 若要运行此示例,还必须在与此代码示例相同的目录中为目标页创建一个文件。To run this sample, you must also create a file for the target page in the same directory as this code example. 目标页的代码在下一个示例中提供。The code for target page is provided in the next example.
<%@ 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 。The following code example demonstrates how to use the Page.PreviousPage property to access a value that was posted from another page using the PostBackUrl property. 此页获取从上一页发送的字符串,并将其显示给用户。This page gets the string that was posted from the previous page and displays it to the user. 如果尝试直接运行此代码示例,则将收到错误,因为该字段的值 text 将为 null 。If you attempt to run this code example directly, you will get an error because the value of the text field will be null. 相反,请使用此代码创建一个目标页,并将该文件放在与上一个示例的代码相同的目录中。Instead, use this code to create a target page and place the file in the same directory as the code for the previous example. 文件的名称必须与 PostBackUrl 上一示例中为属性指定的值相对应。The name of the file must correspond to the value specified for the PostBackUrl property in the previous example. 运行上一示例中的代码时,此页将在出现跨页面 post 时自动执行。When you run the code for the previous example, this page will execute automatically when the cross page post occurs.
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述。For more information, see 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 。The PostBackUrl property allows you to perform a cross-page post using the Button control.
备注
只有正确指定的路径才能使用此属性。Only correctly specified paths work with this property. 例如,相对路径 (Test/default.aspx) , (https://localhost/WebApp/default.aspx) 和虚拟 () 的绝对路径 ~\Test\default.aspx 可以正常工作。For example, relative paths (Test/default.aspx), absolute paths (https://localhost/WebApp/default.aspx) and virtual (~\Test\default.aspx) work correctly. 格式不正确的路径(例如 "/Test/default.aspx" 或 "\Test\default.aspx")不起作用。Incorrectly formed paths such as "/Test/default.aspx" or "\Test\default.aspx" do not work. 有关创建正确路径的讨论,请参阅 ASP.NET Web 项目路径 。See ASP.NET Web Project Paths for a discussion on creating correct paths.
在 PostBackUrl 单击控件时,将属性设置为要发布到的网页的 URL Button 。Set the PostBackUrl property to the URL of the Web page to post to when the Button control is clicked. 例如,指定 Page2.aspx 会导致包含控件的页面 Button 发布到 Page2.aspx 。For example, specifying Page2.aspx causes the page that contains the Button control to post to Page2.aspx. 如果没有为属性指定值 PostBackUrl ,则页面回发到自身。If you do not specify a value for the PostBackUrl property, the page posts back to itself.
重要
使用带有服务器端验证的控件执行跨页面回发时,应检查页面的 IsValid 属性是否在 true 处理回发之前。When performing a cross-page postback with controls with server-side validation, you should check that the page's IsValid property is true before processing the postback. 对于跨页面回发,要检查的页面为 PreviousPage 。In the case of a cross-page postback, the page to check is the PreviousPage. 以下 VB 代码演示了如何执行此操作:The following VB code shows how this is done:
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 中的跨页面发布。For more information on cross-page posting techniques, see Cross-Page Posting in ASP.NET Web Forms.
无法通过主题或样式表主题设置此属性。This property cannot be set by themes or style sheet themes. 有关详细信息,请参阅 ThemeableAttribute 和 ASP.NET 主题和外观。For more information, see ThemeableAttribute and ASP.NET Themes and Skins.