|
下面的代碼示例演示如何使用 PostBackUrl 屬性執(zhí)行跨頁面發(fā)送。 當(dāng)用戶單擊 Button 控件時,頁面會將文本框中輸入的值發(fā)送到 PostBackUrl 屬性指定的目標(biāo)頁。 若要運(yùn)行此示例,您還必須在本代碼示例所在的目錄下創(chuàng)建目標(biāo)頁文件。 目標(biāo)頁的代碼將在下一個示例中提供。 <%@ page language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./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)行此代碼示例,則會發(fā)生錯誤,因?yàn)?text 字段的值將為 Nothing。 正確的做法是使用此代碼創(chuàng)建一個目標(biāo)頁,并將目標(biāo)頁文件與上一示例的代碼放在同一目錄下。 目標(biāo)頁文件名必須與上一示例中為 PostBackUrl 屬性指定的值相對應(yīng)。 當(dāng)運(yùn)行上一示例的代碼時,此頁將在發(fā)生跨頁面發(fā)送時自動執(zhí)行。
<%@ page language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./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./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> |
|
|
來自: 趨明 > 《編程開發(fā)》