主要代碼:
注意這段代碼 是子頁面中添加的也就是彈出的那個頁面刷新父頁面
<script type="text/javascript">
function shuaxin() {
try {
window.parent.opener.location.reload();
window.parent.close();
} catch (e) {
window.parent.opener.location = "http://localhost:32859/test.aspx";
window.parent.opener = null;
window.parent.close();
}
}
</script>
測試demo
首先新建一個web1,再新建一個test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApp02.test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript"> function OpenPage() { window.open('http://localhost:32858/testweb2.aspx', 'newwindow', 'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no') } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblTest" runat="server" Text="還沒開始測試"></asp:Label> <input id="Button1" type="button" value="button" onclick="OpenPage()" /> </div> </form> </body> </html>
test.aspx 后台代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApp02 { public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { lblTest.Text = "頁面執行時間:" + DateTime.Now.ToLocalTime(); } Load(); } public void Load() { lblTest.Text = "頁面執行時間:" + DateTime.Now.ToLocalTime(); } } }
再次新建一個web2,再新建一個testweb2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testweb1.aspx.cs" Inherits="WebApp01.testweb1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript"> function shuaxin() { try { window.parent.opener.location.reload(); window.parent.close(); } catch (e) { window.parent.opener.location = "http://localhost:32859/test.aspx"; window.parent.opener = null; window.parent.close(); } } </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Text1" type="button" value="關閉當前頁面" onclick="shuaxin()" /> </div> </form> </body> </html>
參考文章:http://www.xuebuyuan.com/838707.html
