ASP.NET PostBackUrl属性使用

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值