我正在尝试使用 vb .net 以 asp web 形式跨页发布。 我的目标页面未接收上一页并返回 null。 因此,我在目标页面上得到“无”输出。
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Source.aspx.vb" Inherits="WebApplication2.Source" EnableViewState="true"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Target.aspx" />
</div>
</form>
</body>
</html>
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Target.aspx.vb" Inherits="WebApplication2.Target" EnableViewState="true"%>
<%@ PreviousPageType VirtualPath="~/Source.aspx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Public Class Target
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If PreviousPage IsNot Nothing Then
Dim con As Control = PreviousPage.FindControl("txtname")
Dim txt As TextBox = DirectCast(con, TextBox)
Dim sol As String = txt.Text
Response.Write(sol)
Else
Response.Write("none")
End If
End Sub
End Class
我尝试过使用 EableViewState 属性 true 和 false。但输出仍然没有变化。 我希望将源页面中的文本框中给出的输入显示为目标页面上的输出。
我会在源页面上检查您的代码。我怀疑你有一个按钮事件。
使用您的示例 2 页,那么即使此代码也可以工作:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If PreviousPage IsNot Nothing Then
Dim con As Control = PreviousPage.FindControl("txtname")
Dim txt As TextBox = DirectCast(con, TextBox)
Dim sol As String = txt.Text
Response.Write(sol)
Dim tbox As TextBox = PreviousPage.FindControl("txtname")
Response.Write("<br/> Text Box = " & tbox.Text)
Else
Response.Write("none")
End If
End Sub
结果:
所以,您发布的示例代码确实有效。
所以,不清楚为什么您发布的代码不起作用。
事实上,你甚至可以在 Target.aspx 中省略 this 的页面指令
<%@ PreviousPageType VirtualPath="~/Source.aspx" %>
并且,您发现该页面仍然有效。
另外,请注意源页面上的页面加载仍然会触发!!!
原因是当您在目标页面中引用和使用该页面时,会创建该页面的一个全新实例。
所以,在页面加载中这样说(源代码)。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Debug.Print("page load source")
Debug.Print("Page load source post back = " & IsPostBack)
Debug.Print("Page load source is cross post = " & IsCrossPagePostBack)
End Sub
现在,当我输入一些文本并单击按钮时?
你看到这个:
所以,页面加载确实再次触发!请注意交叉帖子也是如此。
因此,请记住该源页面中的所有内容,包括从源页面加载完成的事件都会触发......
所以,请注意,您为我发布的代码工作得很好。这表明源页面中存在其他页面事件,这些事件在页面加载时正在对 txtname 的源页面文本框控件执行某些操作。