当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET中使用IFRAME建立类Modal窗口

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 52 ::
收藏到网摘: n/a

我们经常要在程序的人机交互中用到模态窗口,但在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

<%@Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="WSGUI1.WebForm1"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
<scriptlanguage="javascript">
functionShowLayer()
{
document.all.MyFormLayer.style.display='';
returnfalse;
}
functionSetURL(url)
{
document.all.IFRAME1.src=url;
}
</script>
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
<FONTface="宋体">
<asp:DropDownListid="DropDownList1"style="Z-INDEX:101;LEFT:40px;POSITION:absolute;TOP:208px"
runat="server"Width="184px">
<asp:ListItemValue="TEST1">q</asp:ListItem>
<asp:ListItemValue="TEST2">w</asp:ListItem>
<asp:ListItemValue="TEST3">e</asp:ListItem>
<asp:ListItemValue="TEST4">r</asp:ListItem>
</asp:DropDownList></FONT><inputtype="button"name="MyButton"value="TEST"id="MyButton"onclick="ShowLayer();SetURL('WebForm2.aspx')"style="Z-INDEX:102;LEFT:360px;POSITION:absolute;TOP:336px">
<divid="MyFormLayer"style="DISPLAY:none;Z-INDEX:103;LEFT:16px;WIDTH:408px;POSITION:absolute;TOP:24px;HEIGHT:304px">
<iframescrolling="no"frameborder="0"width="100%"height="100%"id="IFRAME1"runat="server">
</iframe>
</div>
<asp:Buttonid="Button2"style="Z-INDEX:104;LEFT:256px;POSITION:absolute;TOP:336px"runat="server"
Text="ASPXTest"></asp:Button>
</form>
</body>
</HTML>

WebForm1.aspx.cs

....

publicclassWebForm1:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.DropDownListDropDownList1;
protectedSystem.Web.UI.HtmlControls.HtmlGenericControlIFRAME1;
protectedSystem.Web.UI.WebControls.ButtonButton2;

privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处放置用户代码以初始化页面
if(!IsPostBack)
{

}
}
publicstaticvoidCreateScript(System.Web.UI.Pagemypage,stringstrScript,stringID)
{
stringstrscript="<scriptlanguage='javascript'>";
strscript+=strScript;
strscript+="</script>";
if(!mypage.IsStartupScriptRegistered(ID))
mypage.RegisterStartupScript(ID,strscript);
}
privatevoidButton2_Click(objectsender,System.EventArgse)
{
IFRAME1.Attributes.Add("src","WebForm2.aspx?NAME='中国'");
CreateScript(Page,"ShowLayer();","SHOW");
}
}


WebForm2.aspx

<%@Pagelanguage="c#"Codebehind="WebForm2.aspx.cs"AutoEventWireup="false"Inherits="WSGUI1.WebForm2"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>WebForm2</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
<scriptlanguage="javascript">
functionhide()
{
parent.MyFormLayer.style.display="none";
}
</script>
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form2"method="post"runat="server">
<tableborder="0"width="100%"cellspacing="0"cellpadding="0"bgcolor="#6887bb"height="100%"
id="table1"style="BORDER-TOP-STYLE:outset;BORDER-RIGHT-STYLE:outset;BORDER-LEFT-STYLE:outset;BORDER-BOTTOM-STYLE:outset">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<palign="center"><fontcolor="#ffffff">模仿模态窗口效果</font></p>
<palign="center"><inputtype="button"onclick="hide()"style="WIDTH:80px"value="点击关闭">
<asp:Buttonid="Button1"runat="server"Text="ASPXTest"></asp:Button></p>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</HTML>

WebFom2.aspx.cs

namespaceWSGUI1
{
///<summary>
///WebForm2的摘要说明。
///</summary>
publicclassWebForm2:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.ButtonButton1;

privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处放置用户代码以初始化页面
if(!IsPostBack)
{
Button1.Attributes.Add("onclick","hide()");
}
}

}