How to refresh parent window when child window is closed

Here I will explain how to refresh parent window when child window is closed using JavaScript in asp.net.This functionality will help many developers those who are working with Javascript as well.
To implement this functionality in  your application first create one new website and open Default.aspx page then write the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>http://www.DotnetStock.com</title>
<script type="text/javascript">
function OpenChildWindow() {
window.open('ChildPage.aspx',null,'height=400, width=400, status=yes, toolbar=no, menubar=no, location=center, scrollbar=no');
}
</script>
</head>
<body>
<form id="newform" runat="server">
<div align="center">
<p>This is the parent Window, Click on button to open new window.</p>
<asp:Label ID="lbltext1" runat="server"/>
<asp:Button ID="btnClick1" runat="server" Text="Open Child Window now" OnClientClick="OpenChildWindow()" />
</div>
</form>
</body>
</html>

In code behind Default.aspx.cs add this below given code
protected void Page_Load(object sender, EventArgs e)
{
lbltext1.Text = DateTime.Now.ToString();
}
Now right click on Website -> Select Add New item -> Select Web Form and give name as ChildPage.aspx -> Click OK



Now open ChildPage.aspx and write the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>dotnetstock.com</title>
<script type="text/javascript">
function closechildwindow() {
window.opener.document.location.href = 'Default.aspx';
window.close();
}
</script>
</head>
<body onunload="closechildwindow()">
<form id="newform" runat="server">
<div>
<p>Now close the child window and check that parent window get refreshed</p>
</div>
</form>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *