ASP.NET中使用IFRAME建立类Modal窗口

我们经常要在程序的人机交互中用到模态窗口,但在B/S开发中,这一切变得不容易了,虽然也可以用window.showModalDialog函数实现(见 http://dotnet.aspx.cc/ShowDetail.aspx?id=49ML4AO8-5PB3-4KNY-NJZD-LJOIOXV4M1X4 ),但多数用起来麻烦,还要为了回传值用Frameset建立2个无用的窗口。不爽!

我发现可以尝试在初始页面中嵌入一个IFRAME,然后用IFRAME来显示一个页面,并将IFRAME设定为按绝对位置摆放,Z-Index设置为最高的9999,这样就可以将这个页面覆盖在初始界面上,当需要显示模态窗口时,就显示这个IFRAME,可以将IFRAME的尺寸扩大到能覆盖住初始窗口,也可以盖住关键项,目的就是不让后面的窗口有什么变化的可能。在IFRAME显示的窗口需要关闭时只要对它的parent的IFRAME隐藏就可以了。实际试验时发现IFRAME的diaplay不能在子窗口被改变,所以,我们还需要将IFRAME放到一个DIV中,控制DIV的显示就可以控制窗口的出现或隐藏。但为什么不直接用DIV来显示窗口呢,原因有两个:1.DIV不能遮挡它后面的Dropdownlist控件,而IFRAME能。2.不容易将窗口内的内容放置到一个单独的网页中,复用性差。

以下是代码,显示隐藏使用了客户端和服务端代码两种写法:

WebForm1.aspx

1@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WSGUI1.WebForm1" 
 1<html>
 2<head>
 3<title>WebForm1</title>
 4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
 8<script language="javascript">   
 9function ShowLayer()   
10{   
11document.all.MyFormLayer.style.display='';   
12return false;   
13}   
14function SetURL(url)   
15{   
16document.all.IFRAME1.src=url;   
17}   
18</script>
19</head>
20<body ms_positioning="GridLayout">
21<form id="Form1" method="post" runat="server">
22<font face="宋体">
23<asp:dropdownlist id="DropDownList1" runat="server" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 208px" width="184px">
24<asp:listitem value="TEST1">q</asp:listitem>
25<asp:listitem value="TEST2">w</asp:listitem>
26<asp:listitem value="TEST3">e</asp:listitem>
27<asp:listitem value="TEST4">r</asp:listitem>
28</asp:dropdownlist></font> <input id="MyButton" name="MyButton" onclick="ShowLayer();SetURL('WebForm2.aspx')" style="Z-INDEX: 102; LEFT: 360px; POSITION: absolute; TOP: 336px" type="button" value="TEST"/>
29<div id="MyFormLayer" style="DISPLAY: none;Z-INDEX: 103;LEFT: 16px;WIDTH: 408px;POSITION: absolute;TOP: 24px;HEIGHT: 304px">
30<iframe frameborder="0" height="100%" id="IFRAME1" runat="server" scrolling="no" width="100%">
31</iframe>
32</div>
33<asp:button id="Button2" runat="server" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 336px" text="ASPXTest"></asp:button>
34</form>
35</body>
36</html>

WebForm1.aspx.cs

....

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.HtmlControls.HtmlGenericControl IFRAME1;
protected System.Web.UI.WebControls.Button Button2;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{

}
}
public static void CreateScript(System.Web.UI.Page mypage,string strScript,string ID)
{
string strscript="

1<script language="javascript">";   
2strscript += strScript;   
3strscript += "</script>

";
if(!mypage.IsStartupScriptRegistered(ID))
mypage.RegisterStartupScript(ID, strscript);
}
private void Button2_Click(object sender, System.EventArgs e)
{
IFRAME1.Attributes.Add("src","WebForm2.aspx?NAME='中国'");
CreateScript(Page,"ShowLayer();","SHOW");
}
}

WebForm2.aspx

1@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WSGUI1.WebForm2" 
 1<html>
 2<head>
 3<title>WebForm2</title>
 4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
 8<script language="javascript">   
 9function hide()   
10{   
11parent.MyFormLayer.style.display = "none";   
12}   
13</script>
14</head>
15<body ms_positioning="GridLayout">
16<form id="Form2" method="post" runat="server">
17<table bgcolor="#6887bb" border="0" cellpadding="0" cellspacing="0" height="100%" id="table1" style="BORDER-TOP-STYLE: outset; BORDER-RIGHT-STYLE: outset; BORDER-LEFT-STYLE: outset; BORDER-BOTTOM-STYLE: outset" width="100%">
18<tr>
19<td>
20</td>
21<td>
22</td>
23<td>
24</td>
25</tr>
26<tr>
27<td>
28</td>
29<td>
30<p align="center"><font color="#ffffff">模仿模态窗口效果</font></p>
31<p align="center"><input onclick="hide()" style="WIDTH: 80px" type="button" value="点击关闭"/>
32<asp:button id="Button1" runat="server" text="ASPXTest"></asp:button></p>
33</td>
34<td>
35</td>
36</tr>
37<tr>
38<td>
39</td>
40<td>
41</td>
42<td>
43</td>
44</tr>
45</table>
46</form>
47</body>
48</html>

WebFom2.aspx.cs

namespace WSGUI1
{
///

1<summary>   
2/// WebForm2 的摘要说明。   
3/// </summary>

public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
Button1.Attributes.Add("onclick","hide()");
}
}

}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus