父窗弹出模态窗口子窗,取得父窗值,关闭子窗,同时刷新父窗并返回子窗值到父窗

Father.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Father.aspx.cs" Inherits="WebUI.Temp.Father" %>

<!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 runat="server">
    <title>我是父窗口 Father.aspx ,我用 showModalDialog 方法弹出子窗口 Child.aspx</title>
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>
    <meta http-equiv="Expires" content="no-cache"/>
    <script type="text/javascript" language="javascript"> 
<!--
        
function openChild() {

            
//var k = window.showModalDialog("MainIframe.aspx", window, "dialogWidth:600px;status:no;dialogHeight:500px");
            //以下方法禁止缓存
            var k = window.showModalDialog("MainIframe.aspx?" + (new Date()).valueOf(), window, "dialogWidth:600px;status:no;dialogHeight:500px")

            
if (k != null)
                document.getElementById(
"txbFather").value = k;
        } 
//--> 
</script> 
</head>
<body>
    <form id="form1" runat="server">
        <table border="1" cellpadding="0" cellspacing="0" width="600">
            <tr bgcolor="#ccccff">
                <td>
                    1、传递到父窗口的值:<br />
                    <br />
                    2、并能取得子窗口传递进来的值</td>
                <td>
                    <asp:TextBox  ID="txbFather" runat="server" Width="200px">我是父窗的初始值</asp:TextBox>
                </td>
            </tr>
         
            <tr bgcolor="#66cc66">
                <td height="40">
                   <input type ="button" value="弹出子窗口" onclick="openChild()" />
                   </td>
                <td>
               
                </td>
            </tr>
           
        </table>
    </form>
</body>
</html>

MainIframe.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainIframe.aspx.cs" Inherits="WebUI.Temp.MainIframe" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <iframe src="Child.aspx" width="100%" height="100%"  frameborder="0" ></iframe>
    </form>
</body>
</html>

Child.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="WebUI.Temp.Child" %>

<!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 runat="server">
    <title>我是子窗口 Child.aspx ,用showModalDialog弹出的子窗口</title>
    <base target="_self"/>
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>
    <meta http-equiv="Expires" content="no-cache"/>
</head>
<body>
    <form id="form1" runat="server">
    <table border="1" cellpadding="0" cellspacing="0" width="600">
       
          <tr bgcolor="#ff66cc">
            <td height="50">
               <input type ="button" value="一、用传统的 input 关闭且返回值" onclick="retrunValue()" />
             </td>
              <td>
               取得父窗口传递进来的值
                <asp:TextBox ID="txbChild" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr bgcolor="#3399ff">
            <td  height="50">
                <asp:Button  ID="Button1" runat="server" onclick="Button1_Click" Text="二、用服务器控件 Button1 关闭且返回值" />
            </td>
            <td>
             可以改变上面 TextBox 的值,返回给父窗口
            </td>
        </tr>
        
    </table>
   
    </form>
</body>
</html>
<script type="text/javascript" language="javascript"> 
<!--
    
//var k = window.dialogArguments;
    var k = window.parent.dialogArguments;
    
//当前页中 txbChild 获得父窗口 txbFather 传递来的值 
    if (k != null) {
        document.getElementById(
"txbChild").value = k.document.getElementById("txbFather").value;
    }

    
//将当前页 txbChild 中的值 返回到父窗口中,并关闭当前页
    function retrunValue() {
        
var s = document.getElementById("txbChild").value;
        window.parent.returnValue 
= s;
        window.parent.close();
    } 
//--> 
</script> 

Child.aspx.cs

 protected void Button1_Click(object sender, EventArgs e)
        {

            System.Text.StringBuilder cstext2 = new System.Text.StringBuilder();
            cstext2.Append(string.Format("<script type=text/javascript>var s ='{0}'; "this.txbChild.Text.ToString()));
            cstext2.Append("window.returnValue = s;window.close();</");
            cstext2.Append("script>");
            Response.Write(string.Format("{0}", cstext2.ToString()));


        }

 Parent.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Parent.aspx.cs" Inherits="WebUI.Temp.Parent" %>

<!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 runat="server">
    <title></title>
    <script type="text/javascript" language="javascript"> 
<!--
        
function openChild() {

            
var k = window.showModalDialog("SubParent.aspx", window, "dialogWidth:335px;status:no;dialogHeight:300px");
            
if (k == 1)//判断是否刷新 
            {
                alert(
'刷新');
                window.location.reload();
            }
        } 
//--> 
</script> 
</head>
<body>
    <form id="form1" runat="server">
   <br>传递到父窗口的值:<input id="txt9" type="text" value="3333333333333" /><br> 
   <input type ="button" value="openChild" onclick="openChild()" /> 
    </form>
</body>
</html>

SubParent.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SubParent.aspx.cs" Inherits="WebUI.Temp.SubParent" %>

<!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 runat="server">
    <title></title>
    <base target="_self" />
<meta http-equiv="Expires" CONTENT="0" /> 
<meta http-equiv="Cache-Control" CONTENT="no-cache" /> 
<meta http-equiv="Pragma" CONTENT="no-cache" /> 
</head>
<body>
    <form id="form1" runat="server">
   <br>父窗口传递来的值:<input id="txt0" type="text"><br> 

<input type ="button" value="关闭刷新父窗口" onclick="winClose(1)"> 
<input type ="button" value="关闭不刷新父窗口" onclick="winClose(0)">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text
="服务器端关闭并刷新父页" />
    </form>
</body>
</html>
<script type="text/javascript" language=javascript> 
<!--
    
var k = window.dialogArguments;
    
//获得父窗口传递来的值 
    if (k != null) {
        document.getElementById(
"txt0").value = k.document.getElementById("txt9").value;
    }

    
//关闭窗口返回是否刷新的参数. 
    function winClose(isRefrash) {

        window.returnValue 
= isRefrash;
        window.close();
    } 
//--> 
</script> 

SubParent.aspx.CS

 protected void Button1_Click(object sender, EventArgs e)
        {

            string isFresh = "1";
            System.Text.StringBuilder cstext2 = new System.Text.StringBuilder();
            cstext2.Append(string.Format("<script type=text/javascript> var s ='{0}'; ", isFresh));
            cstext2.Append("window.returnValue = s;window.close();</");
            cstext2.Append("script>");
            Response.Write(string.Format("{0}", cstext2.ToString()));
        }

例子代码下载 :/Files/tiger8000/Sample.rar

原文地址:https://www.cnblogs.com/tiger8000/p/2398192.html