Tuesday, November 6, 2012

How to refresh parent form when child form closed - Java Script

Introduction:

Here I am going to show how to refresh parent window when child window is closed using Java script in Asp.net. 

Step 1: Add a new web form in your web application named parent.aspx and write the following java script function in source.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Parent Form</title> 
   
 <script type="text/javascript">
    function OpenChildWindow() {
//      To open a new browser window, use the window.open() method.
        window.open('ChildPage.aspx'null'height=300, width=300, status=yes, toolbar=no, menubar=no, location=center, scrollbar=no');
    }
    </script> 
</head>

<body>
    <form id="form1" runat="server">
    <div align="center">
        <p>
            Click on button to open new window</p>
        <asp:Label ID="lbltext" runat="server" />
        <asp:Button ID="btnClick" runat="server" Text="Open Child Window"OnClientClick="OpenChildWindow()" />
    </div>
    </form>
</body>
</html>

Code Behind:

protected void Page_Load(object sender, EventArgs e)
    {
        // To Display Current Date:
        lbltext.Text = DateTime.Now.ToString();
    }


Step 2: Secondly add another new web form named Child.aspx and add the following code:.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<title>Child Form</title>

    <script type="text/javascript">
    function closechildwindow() {
        window.opener.document.location.href = 'parent.aspx';
        window.close();
    }
    </script>

</head>

<body onunload="closechildwindow()">
    <form id="form1" runat="server">
    <div>
        <p>
            Now close the child window and notice that parent page get changed</p>
    </div>
    </form>
</body>
</html>

Output:
ASP.net, C#, Java Script
Add caption

Click here to download source code:

1 comment :