程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> ASP.NET中使用IFRAME建立類Modal窗口代碼

ASP.NET中使用IFRAME建立類Modal窗口代碼

編輯:關於C#
 

我們經常要在程序的人機交互中用到模態窗口,但在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


<%@ 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 name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<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 id="Form1" method="post" runat="server">
<FONT face="宋體">
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 208px"
runat="server" Width="184px">
<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" name="MyButton" value="TEST" id="MyButton" onclick="ShowLayer();SetURL('WebForm2.aspx')" style="Z-INDEX: 102; LEFT: 360px; POSITION: absolute; TOP: 336px">
<div id="MyFormLayer" style="DISPLAY: none;Z-INDEX: 103;LEFT: 16px;WIDTH: 408px;POSITION: absolute;TOP: 24px;HEIGHT: 304px">
<iframe scrolling="no" frameborder="0" width="100%" height="100%" id="IFRAME1" runat="server">
</iframe>
</div>
<asp:Button id="Button2" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 336px" 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>  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved