刷新modalDialog的父窗口(转)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  
<html>
      
<head>
          
<title></title>
          
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
          
<meta name="ProgId" content="VisualStudio.HTML">
          
<meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
          
<script language="javascript">
              
<!--
             
/**
             * 用途:打开模态窗口,并且当返回值为true时,刷新窗口
            * 
           * @param newUrl        模态窗口页面路径
            * @param wLen        模态窗口宽度
           * @param iLen        模态窗口高度
           *
*/

             
function OpenModalDialog(newURL,wLen,hLen)
             
{
                 
try
                
{
                    
//初始化变量,用于接收页面反回值。
                     var recdata=false;
                     
//模式窗口打开指定的窗口链接
                     recdata=showModalDialog(newURL,"DescWindow",
                         
"dialogWidth:"+wLen+"px;dialogHeight:"+hLen+"px;center:1;scroll:1;help:0;status:0");
 
                     
//判断对应的返回值
                     if(recdata==true)
                     
{
                         
// 刷新当前窗口
                         window.location.reload();
                         window.alert(
"刷新窗口~~");
                     }

                 }

                 
catch(err)
                
{}
             }

             
-->
     
</script>
     
</head>
     
<body>
         
<href="javascript: OpenModalDialog('sub.aspx', 700, 500)">打开模态窗口</a>
     
</body>
 
</html>

2、建立页面sub.aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestSyntax.WebForm1" %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 
<HTML>
      
<HEAD>
          
<title>sub</title>
         
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
          
<meta content="C#" name="CODE_LANGUAGE">
          
<meta content="JavaScript" name="vs_defaultClientScript">
          
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
         
<base target="_self">
     
</HEAD>
     
<body>
         
<form id="Form1" method="post" runat="server">
             
<asp:Button id="btnOk" runat="server" Text="确 定"></asp:Button>&nbsp; 
             
<INPUT type="button" value="取 消" onclick="javascript: window.close();">
         
</form>
     
</body>
 
</HTML>
3、sub.aspx.cs代码如下
  using System;
  
using System.Collections;
  
using System.ComponentModel;
  
using System.Data;
  
using System.Drawing;
  
using System.Web;
  
using System.Web.SessionState;
  
using System.Web.UI;
  
using System.Web.UI.WebControls;
 
using System.Web.UI.HtmlControls;
 
 
namespace TestSyntax
 
{
     
/// <summary>
     
/// WebForm1 的摘要说明。
     
/// </summary>

     public class WebForm1 : System.Web.UI.Page
     
{
         
protected System.Web.UI.WebControls.Button btnOk;
     
         
private void Page_Load(object sender, System.EventArgs e)
         
{
             
// 在此处放置用户代码以初始化页面
         }

 
         
Web 窗体设计器生成的代码
 
         
/// <summary>
         
/// 操作成功,设定模态窗口的返回值为true,用来刷新父窗口
         
/// </summary>
         
/// <param name="sender"></param>
         
/// <param name="e"></param>

         private void btnOk_Click(object sender, System.EventArgs e)
         
{
             
string strScript = @"<script>window.returnValue=true;window.close();</script>";
 
             
this.Response.Write(strScript);
         }

     }

 }

这样,实现了操作成功后刷新父窗口,又避免了取消操作的无用刷新。


Web开发中适当运用一些弹出子窗口有很多好处,可以节省页面设计代价,获得好的用户体验,在最近项目开发中我遇到了几个父子窗口的问题,现在整理给大家,希望有所帮助.
              
   情景一: 打开某一子窗口, 子窗口中任一按钮点击时候不能弹出新页面,进行完操作后,关闭该子窗口,刷新父窗口.       
      
                 1: 页面A:父窗口,其中有一个打开子窗口的链接,<a  href="#"onclick="open()">页面C</a>
                      A中有如下js代码: <script language="JavaScript">
   function open()
   {
     window.showModalDialog("页面B");
   }
</script>
                2: 页面B,此为中间页,起过渡作用
                     B html 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>**</title>
</head>

<frameset rows="0,*">
    <frame src="about:blank">
    <frame src="页面C">
  </frameset><noframes></noframes>
</html>

                 3:页面C ,要打开的子窗口.
                    它关闭时候刷新父窗口很简单,只要把A中
                    <a  href="#"onclick="open()">页面C</a>  改为
                    <a  href="页面A"onclick="open()">页面C</a>

    情景二:打开某一需要用到activex控件子窗口, 子窗口中任一按钮点击时候不能弹出新页面,进行完操作后,关闭该子窗口,刷新父窗口. 
                  此时候就不能用 window.showModalDialog()事件打开模式对话框了,因为activex控件会报错,必须用window.open()
 
                  1: 页面A:父窗口,其中有一个打开子窗口的链接,<a  href="#"onclick="open()">页面B</a>
                      A中有如下js代码.


<script language="JavaScript">
function open()
 {
   window.open("页面B",'upload', 'height=400, width=420, top=0, left=0, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no');
 }
</script>


                  2: 页面B,要打开的子窗口,关闭时候触发window.opener.location.reload();window.close();即可刷新父窗口并且关闭子窗口.

  
     情景三:打开某一子窗口,   让用户选择要添加的东东,譬如要添加到文章里的相片选择后关闭子窗口,然后选择的东东出现在父窗口里.
                    在下图中,我点击"添加照片"链接然后弹出子窗口,在子窗口中选择后点击"添加照片"按钮,子窗口自动关闭,并且父窗口"已添加照片:"下面列出了我选择的照片。

       
     
           

               实现方法:类似情景一需要中间页面B , 只是子窗口C中点击"添加按钮"时触发的js事件中,除了获得选中的checkbox的值外,还要把获得的值回传给父窗口,传值回去的代码如下.
             

window.parent.returnValue="选中的checkbox";
               window.parent.close();
 
              而父窗口要捕获此值就要在情景一中所说的open()事件中添加获得返回值 

<script language="JavaScript">
  function open()
   {
         var str=window.showModalDialog("页面C");
         if(str!=null) 
       {             
          picobj.innerHTML+=str;
         
       }
    }
</script>
               注意这里的str是获取的返回值, 而picobj是你要显示被选择东东所放位置的div的id ,这里是<div id=picobj></div>


posted @ 2007-09-25 14:19  江湖工夫  阅读(2191)  评论(0编辑  收藏  举报