下面的代码示例演示如何使用 PostBackUrl 属性执行跨页面发送。 当用户单击 Button 控件时,页面会将文本框中输入的值发送到 PostBackUrl 属性指定的目标页。 若要运行此示例,您还必须在本代码示例所在的目录下创建目标页文件。 目标页的代码将在下一个示例中提供。
<%@ 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 属性从其他页发送的值。 该页获取从上一页发送的字符串,并将其显示给用户。 如果尝试直接运行此代码示例,则会发生错误,因为 text 字段的值将为 Nothing。 正确的做法是使用此代码创建一个目标页,并将目标页文件与上一示例的代码放在同一目录下。 目标页文件名必须与上一示例中为 PostBackUrl 属性指定的值相对应。 当运行上一示例的代码时,此页将在发生跨页面发送时自动执行。
安全说明 |
此示例有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关更多信息,请参见脚本侵入概述。 |
<%@ 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>
|