实现:父窗口点击提交按钮调用showModalDialog显示子窗口,子窗口中任意按钮(除浏览器右上角“X”外)关闭子窗口并继续执行父窗口提交事件。当点击浏览器右上角“X”,返回到父窗口,而不执行提交事件。
Logon.aspx
<script language="javascript">
function ShowPlatformSelected()
{
CloseMenu();
var url = "PlatformSelected.aspx";
//屏蔽浏览器上所有按钮(除浏览器右上角“X”外)
var params=window.showModalDialog(url,"","dialogHeight: 400px; dialogWidth: 500px;edge: Raised; center: Yes;resizable: No; status: No;help:No;scroll:No");
if(params=="close")
{
document.Form1.all("hdModule").value = "close";
}
}
</script>
<asp:textbox Width="160px" id="tbID" runat="server" CssClass="TextBox"></asp:textbox>
<asp:textbox Width="160px" id="tbPwd" runat="server" CssClass="TextBox" TextMode="Password"></asp:textbox>
<asp:button id="btnLogon" runat="server" Width="80px" CssClass="Button"></asp:button>
<input type="hidden" id="hdModule" name="module" value="" runat="server">
Logon.aspx.cs
Load页面时注册btnLogon的onclick脚本
private void Page_Load(object sender, System.EventArgs e)
{
if(string.Compare(SystemParameters.ShowPlatformSelected,"T",true)==0)
btnLogon.Attributes.Add("onclick", "ShowPlatformSelected()");
}
private void btnLogon_Click(object sender, System.EventArgs e)
{
//如果showModalDialog的反回值为close,则终止执行btnLogon_Click
if (hdModule.Value.ToLower() == "close") {
tbID.Text = string.Empty;
return;
}
......
}
onclick事件会在btnLogon_Click前执行
PlatformSelected.aspx
默认返回值为"close"
<SCRIPT language="JavaScript">
function init(){
window.returnValue="close";
}
</SCRIPT>
<body class="main_body" leftMargin="0" topMargin="0" onload="init();">
PlatformSelected.aspx.cs
点击模式对话框中其他按钮时触发事件,返回值"module",导致点击浏览器右上角“X”时返回值为"close"。以此区别是点击浏览器右上角“X”,或是其他功能按钮
private void lbtnPlat_Click(object sender, System.EventArgs e)
{
......
Response.Write("<script language=javascript>window.returnValue='module';window.opener=null;window.close();</script>");
}
protected void Page_Load(object sender, EventArgs e) {
ImageButton image = new ImageButton();
image.Click += new ImageClickEventHandler(lbtnPlat_Click);
.......
}
****************************************************************
不知道为什么以上方法会存在问题,即点击页面上除(X)外的按钮时,第一次会返回“module”,之后就会返回值“close”。
想想应该是和Asp.Net页面的加载机制有关。页面在第一次加载时会执行OnInit、Page_Load事件;之后再次出现该页面时不会加载这两个事件,而在点击按钮时加载OnInit、Page_Load事件。
后改为Session传值的方式解决,是点击页面右上角X,还是点击其他按钮。
PlatformSelected.aspx.cs
protected void Page_Load(object sender, EventArgs e) {
Session.Add("Platforms", "close");
ImageButton image = new ImageButton();
image.Click += new ImageClickEventHandler(lbtnPlat_Click);
.......
}
private void lbtnPlat_Click(object sender, System.EventArgs e) {
.......
if (Session["Platforms"] != null)
Session["Platforms"] = platforms;
else
Session.Add("Platforms", platforms);
Response.Write("<script language=javascript>window.opener=null;window.close();</script>");
}
Logon.aspx.cs
private void btnLogon_Click(object sender, System.EventArgs e)
{
if (Session["Platforms"] == null || Session["Platforms"].ToString() == "close") {
tbID.Text = string.Empty;
return;
}
......
}