Friday, December 13, 2013

Refresh parent window from child window on close using javascript

Following code snippets will guide you Refresh parent window from child window on close using JavaScript.

Parent Window
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ParentWindow.aspx.cs" 
Inherits="RefreshParentWindow.ParentWindow" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Parent Window</title>
    <script type="text/javascript">
        var popup;
        function ShowChildWindow(url) {
            popup = window.open(url,
            "new_window",
            "width=300,height=500,left=200px, top=200px");
            popup.focus();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <h3>Parent Window</h3>
        <div>
            <b>Last refreshed On:</b>
            <%=DateTime.Now %>
        </div>
        <br />
        <input type="button" value="Show Child Window" 
        onclick="ShowChildWindow('ChildWindow.aspx');" />
    </form>
</body>
</html>

Child Window
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChildWindow.aspx.cs" 
Inherits="RefreshParentWindow.ChildWindow" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Child Window</title>
    <script type="text/javascript">
        function RefreshParentWindow() {
            if (window.opener != null) {
                window.opener.location.reload();
            }
        }
        window.onbeforeunload = RefreshParentWindow;
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <h3>Child Window</h3>
        <br />
        <p> Click close button to refresh parent page.</p>
    </form>
</body>
</html>

Sample
Download

No comments:

Post a Comment