关闭子页面刷新父页面(前台或后台控制)及打开子页面关闭父页面(不提示!)的例子

注:三个页面(编写三个前台,两个后台),可以验证两种方式(前台、后台)关闭子页面刷新父页面,并验证打开子页面关闭父页面(不提示!)

--Parent.aspx(前台)

-------------------------------------------------------------------省略------------------------------------------------------------------

<head runat="server">    

  <title></title>    

  <script type="text/javascript">      

           //弹出框的高、宽以及居中
           var height = 400;
           var width = 600;
           var top = (window.screen.height - 30 - height) / 2;
           var left = (window.screen.width - 10 - width) / 2;  

 

      //打开子页面        

      function openChildren() {            

        window.open('Children.aspx', 'Childrenwindow', 'height=' + height + ',width=' + width + ',top=' + top +',left=' + left + ',toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no, status=no');    

      }

 

           //父页面重新刷新(后台JS调用)        

      function refresh() {            

        location = location;        

      }

 

           //打开子页面关闭父页面        

      function openChildrenCloseParent() {                   

                 window.open('OtherChildren.aspx', 'Childrenwindow', 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ',toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no, status=no');     

        window.opener = null;     

        window.open('', '_self'); //IE6 以上加 上这行             

        window.close();        

       }

  </script>

</head>

<body>    

  <form id="form1" runat="server">    

    <div>        

      <input id="Parent" type="button" value="打开子页面"  onclick="openChildren()" />        

      <asp:Button ID="ParentBack" runat="server" Text="打开子页面(后台)"            

        onclick="ParentBack_Click"  />       

      <input id="Button1" type="button" value="打开子页面关闭父页面"  onclick="openChildrenCloseParent()" />    

    </div>    

  </form>

</body>

-------------------------------------------------------------------省略------------------------------------------------------------------

 

--Parent.aspx.cs(后台)

-------------------------------------------------------------------省略------------------------------------------------------------------

        protected void ParentBack_Click(object sender, EventArgs e)
        {           

     //弹出框的高、宽以及居中            

     int height = 400;            

     int width = 600;            

     int top = (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height -30 - height) / 2;            

     int left= (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width -10 - width) / 2;

 

            Response.Write("<script>window.open('Children.aspx', 'Childrenwindow', 'height=" + height + ",width=" + width + ",top=" + top + ",left=" + left + ",toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no, status=no');</script>");
        }

-------------------------------------------------------------------省略------------------------------------------------------------------

 

--Children.aspx(前台)

-------------------------------------------------------------------省略------------------------------------------------------------------

<head runat="server">    

  <title></title>    

   <script type="text/javascript">        

     //子页面关闭刷新父页面
       function ChildrenCloseRefreshParent() {
            window.close();
              window.opener.location = window.opener.location;
        }

  </script>

</head>

<body>    

  <form id="form1" runat="server">    

    <div>        

      <input id="Children" type="button" value="关闭子页面并刷新父页面"  onclick="ChildrenCloseRefreshParent()" />
           <asp:Button ID="ChildrenBack" runat="server" Text="关闭子页面并刷新父页面(后台打开请点击)" 
               onclick="ChildrenBack_Click" />

    </div>    

  </form>

</body>

-------------------------------------------------------------------省略------------------------------------------------------------------

 

--Children.aspx.cs(后台)

-------------------------------------------------------------------省略------------------------------------------------------------------

        protected void ChildrenBack_Click(object sender, EventArgs e)
        {

     //关闭子页面并刷新父页面
          Response.Write("<script>window.opener.refresh();window.opener=null;window.close();</script>");
        }

-------------------------------------------------------------------省略------------------------------------------------------------------

 

--OtherChildren.aspx(打开子页面关闭父页的子页面)

-------------------------------------------------------------------省略------------------------------------------------------------------

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
      <div style="text-align:center">
          <label>你成功了吗?</label>
      </div>
    </form>
</body>

-------------------------------------------------------------------省略------------------------------------------------------------------

posted @ 2013-08-29 21:35  菜鸟青年  阅读(570)  评论(0)    收藏  举报