-
很少使用window.open()方法。做了一个打开自适应子窗体,关闭子窗体刷新父窗体的DEMO。
父窗体ParentWin.aspx

Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>

<script language="javascript" type="text/javascript">
// <!CDATA[


function btnOpen_onclick()
{

window.open("ChildWin.aspx",500,300);
// window.open("ChildWin.aspx","url","toolbar=no,resizable=no,scrollbars=yes,location=no, status=no");
}
</script>
</head>
<body >
<form id="form1" runat="server">
<div>
<input id="btnOpen" type="button" value="打开子窗体" onclick="return btnOpen_onclick()" />
<asp:TextBox ID="txtBox" runat="server"></asp:TextBox>
<input id="Submit1" type="submit" value="" style="display:none;" />
</div>
</form>
</body>
</html>

ParentWin.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
txtBox.Text = System.DateTime.Now.ToString();//验证页面刷新
}

Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>

<script language="javascript" type="text/javascript">
// <!CDATA[


function btnClose_onclick()
{


self_ReplaceParent(1);
window.close();
}

//刷新父窗体,并关闭

function self_ReplaceParent(bool)
{

if(bool == 1)
{

if(opener != null)
{
if (opener.document.getElementById("Submit1") != null)
opener.document.getElementById("Submit1").click();
else
opener.window.location.reload();
}
}
window.close();
}
//页面加载时自动调整大小
function win_onLoad()


{

var table = document.body.getElementsByTagName("table");
var tb = table[0];
var width = 752;
if(tb != null)

{
width = tb.width;
}
// alert(tb.offsetHeight);
var height = tb.offsetHeight;
// alert(width);
width = eval(width * 1 + 30);
height = eval(height * 1 + 60);
var sWidth = screen.width;
var sHeight = screen.height;

var left = sWidth-((sWidth-width)/2 + width);
if(left < 0)

{
left = 0;
}
var top = sHeight-((sHeight-height)/2 + height);
if(top < 0)

{
top = 0;
}
//alert(width);
if (width > screen.width-50)
width = screen.width-50;
if (height > screen.height-50)
height = screen.height-50;
//alert(width);
window.moveTo(left, top);
window.resizeTo(width+20,height);
}

// ]]>
</script>
</head>
<body onload="win_onLoad();">
<form id="form1" runat="server">
<div>
<input id="btnClose" type="button" value="客户端注册脚本" onclick="return btnClose_onclick()" />
<asp:Button ID="btnCloseServer" runat="server" OnClick="btnCloseServer_onclick" Text="服务器端注册脚本" />
<table border="0" cellpadding="0" cellspacing="0" width="750px">
<tr>
<td>
</td></tr></table>
</div>
</form>
</body>
</html>

ChildWin.aspx.cs
protected void btnCloseServer_onclick(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "error", "<script language='javascript'>alert('子窗口!');self_ReplaceParent(1);window.close();</script>");
}
可以再借助AJAX UpdatePanel,刷新父窗体的局部页面,给用户带来更好的体验。
<打开模式窗体:window.showModalDialog('http://www.17vs.com',null,'toolbar=no,resizable=no,scrollbars=yes,location=no, status=no');">
如果打开的是模式子窗体,要在子窗体的head标签里加上<base target="_self">,否则在处理服务器端控件的时候,会弹出新窗体!