我正在尝试做一些必须通过 aspx 服务器端进行的框架导航。
这是我正在尝试做的一个简单示例:
pg1.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pg1.aspx.vb" Inherits="pg1" %>
<html>
<body>
<frameset>
<frame src="pg2.aspx" id="pg2"/>
<frame src="pg3.aspx" id="pg3"/>
</frameset>
</body>
</html>
pg2.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pg2.aspx.vb" Inherits="pg2" %>
<html>
<head>
<script>
alert(parent.frames['pg3'].frames['pg5'].location.href);
</script>
</head>
<body>
<asp:LinkButton id="lkBt" runat="server"/>
</body>
</html>
pg2.vb
Public Partial Class pg2
Inherits System.Web.UI.Page
Protected Sub lkBt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lkBt.Click
Response.Write("<script>" & vbCrLf)
Response.Write("parent.frames['pg3'].frames['pg5'].location.href = 'pg6.aspx';")
Response.Write(vbCrLf & "</script>")
End Sub
End Class
pg3.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pg3.aspx.vb" Inherits="pg3" %>
<html>
<body>
<frameset>
<frame src="pg4.aspx" id="pg4"/>
<frame src="pg5.aspx" id="pg5"/>
</frameset>
</body>
</html>
pg6.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pg6.aspx.vb" Inherits="pg6" %>
<html>
<body>
<p>pg6</p>
</body>
</html>
问题出在 pg2.vb 中,当响应写入时,我收到一个 javascript 错误消息:
parent.frames.pg3.frames.pg5.location.href 为空或不是对象
这很奇怪,因为正如您在 pg2.aspx 中看到的那样,我已经放置了相同 href 的测试警报:
警报(parent.frames['pg3'].frames['pg5'].location.href);
这会在没有任何错误的情况下提醒预期的链接。
这似乎与 aspx 运行的 response.write 上下文有关,我之所以这么说是因为在浏览器的调试中,当我在 pg2.vb 响应写入错误生成的断点处停止时,在页面/脚本选择中只有 pg2 .aspx。但是当我调试并在 pg2.aspx head>script>alert 处设置断点时,我可以看到选择器中的所有页面。
有没有人知道如何让 lkBt_click>Response.Write 在所有页面的上下文中运行?