下面的代碼示例演示如何使用 PostBackUrl 屬性執(zhí)行跨頁面發(fā)送。 當(dāng)用戶單擊 Button 控件時(shí),頁面會(huì)將文本框中輸入的值發(fā)送到 PostBackUrl 屬性指定的目標(biāo)頁。 若要運(yùn)行此示例,您還必須在本代碼示例所在的目錄下創(chuàng)建目標(biāo)頁文件。 目標(biāo)頁的代碼將在下一個(gè)示例中提供。
<%@ 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 屬性從其他頁發(fā)送的值。 該頁獲取從上一頁發(fā)送的字符串,并將其顯示給用戶。 如果嘗試直接運(yùn)行此代碼示例,則會(huì)發(fā)生錯(cuò)誤,因?yàn)?text 字段的值將為 Nothing。 正確的做法是使用此代碼創(chuàng)建一個(gè)目標(biāo)頁,并將目標(biāo)頁文件與上一示例的代碼放在同一目錄下。 目標(biāo)頁文件名必須與上一示例中為 PostBackUrl 屬性指定的值相對(duì)應(yīng)。 當(dāng)運(yùn)行上一示例的代碼時(shí),此頁將在發(fā)生跨頁面發(fā)送時(shí)自動(dòng)執(zhí)行。
![]() |
---|
此示例有一個(gè)接受用戶輸入的文本框,這是一個(gè)潛在的安全威脅。 默認(rèn)情況下,ASP.NET 網(wǎng)頁驗(yàn)證用戶輸入是否不包含腳本或 HTML 元素。 有關(guān)更多信息,請(qǐng)參見腳本侵入概述。 |
<%@ 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>
聯(lián)系客服