By default, if you click a button control, the page containing the control is posted back to itself and the same page is reloaded. However, you can use the PostBackUrl property to post form data to another page.
For example, the page in Listing 2.20 includes a search form. The Button control in the page posts the form to another page named ButtonSearchResults.aspx. The
ButtonSearchResults.aspx page is contained in Listing 2.21.
上面这段原版英文是摘抄自 ASP.NET 3.5 揭秘关于Button控件的阐述,现在我把它翻译一下,个别地方可能不够准确:
默认情况下,如果你单击一个Button控件,包含这个控件的页面将会回发自己的内容(到服务器),同时重新加载自己本身。然而,你可以使用PostBackUrl属性将当前表单数据提交至另一个页面,例如在代码清单2.20包含了一个search的表单页,(利用)Button控件将页面发送至另外一个页面名字是ButtonSearchResults.aspx的网页.....
现在我将代码分别出来,显示PostBackUrl属性的作用:
------------------------------------------------------------------------------------------------------------
Souce Code2.20(C#)
<%@ 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 Search</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblSearch" Text="Search:" runat="server" />
<asp:TextBox ID="txtSearch" runat="server" />
<asp:Button ID="btnSearch" Text="Go!" PostBackUrl="ButtonSearchResults.aspx" runat="server" />
</div>
</form>
</body>
</html>
Sorcere Code 2.21 (C# )
<%@ 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()
{
if(PreviousPage != null)
{
TextBox txtSearch = (TextBox)PreviousPage.FindControl("txtSearch");
lblSearch.Text = String.Format("Search For: {0}" ,txtSearch.Text);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Button Search Results</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblSearch" runat="server" />
</div>
</form>
</body>
</html>