加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

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

发布时间:2021-07-17 20:12:22 所属栏目:编程 来源:互联网
导读:ASP.NET中使用IFRAME建立类Modal窗口
副标题[/!--empirenews.page--]

我们经常要在程序的人机交互中用到模态窗口,但在B/S开发中,这一切变得不容易了,虽然也可以用window.showModalDialog函数实现(见?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

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WSGUI1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta Content="Microsoft Visual Studio .NET 7.1">
  <meta Content="C#">
  <meta content="JavaScript">
  <meta content="">
  <script language="javascript">
   function ShowLayer()
   {
    document.all.MyFormLayer.style.display='';
    return false;
   }
   function SetURL(url)
   {
    document.all.IFRAME1.src=url;
   }
  </script>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form method="post" runat="server">
   <FONT face="宋体">
    <asp:DropDownList
     runat="server">
     <asp:ListItem Value="TEST1">q</asp:ListItem>
     <asp:ListItem Value="TEST2">w</asp:ListItem>
     <asp:ListItem Value="TEST3">e</asp:ListItem>
     <asp:ListItem Value="TEST4">r</asp:ListItem>
    </asp:DropDownList></FONT> <input type="button" value="TEST">
   <div>
    <iframe scrolling="no" frameborder="0" runat="server">
    </iframe>
   </div>
   <asp:Button runat="server"
    Text="ASPXTest"></asp:Button>
  </form>
 </body>
</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="<script language='javascript'>";
   strscript += strScript;
   strscript += "</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

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WSGUI1.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm2</title>
  <meta Content="Microsoft Visual Studio .NET 7.1">
  <meta Content="C#">
  <meta content="JavaScript">
  <meta content="">
  <script language="javascript">
  function hide()
  {
   parent.MyFormLayer.style.display = "none";
  }
  </script>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form method="post" runat="server">
   <table cellspacing="0" cellpadding="0" bgcolor="#6887bb"
   >
    <tr>
     <td>
     </td>
     <td>
     </td>
     <td>
     </td>
    </tr>
    <tr>
     <td>
     </td>
     <td>
      <p><font color="#ffffff">模仿模态窗口效果</font></p>
      <p><input type="button" value="点击关闭">
       <asp:Button 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

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读