Refreshing a parent page from a child in ASP.NET 2.0


Summary:

In this article, I'm going to explain how to refresh a parent page from a child page using the new API of ASP.NET 2.0 and JavaScript language. Sometimes, it is required to provide a popup windows showing detail information of one business entity among several ones residing in a parent page for example inside a Repeater control, and when the former business entity's properties change, the content in the parent page must be updated.

Solution.

You create a parent page with Repeater control where it is presented several business entities in a resumed way, and a reference to a child page showing detailed information of each one. You can do that through JavaScript code and generating the proper reference using <a> tag. It is supposed that you define DataSet and DataTable objects for some business entity's schemas residing in a RDBMS which has a property named Id. In Visual Studio .NET is very simple using XSD, and related tools of "Data Sources" view.

<a href="javascript:OpenWindow('childpage.aspx?id=<%# DataBinder.Eval(Container.DataItem,"Id")%>')>Click for detail information</a>

<script language="javascript" type="text/javascript">

function OpenWindow(strChildPageUrl)

{

window.open (strChildPageUrl, 'New Page','width=600px,height=800px,top=0,left=0,scrollbars=1');

}

</script>
 

The OpenWindow function is triggered when the user click on the reference which causes to open a new popup windows.

Refreshing the parent page.

 

If you change something in the child windows, the changes will not be reflected in the parent windows until it's refreshed. The user can click on the "Refresh" button in the web browser but it's not a good approach. One simple solution is to add the following client-side code:

window.opener.document.forms[0].submit();

The only drawback is that if you have some Validators controls on the page, the parent page is not submitted.

In ASP.NET 2.0 you can add this client-side code through the Page.ClientScript object instance of the class ClientScriptManager. So, in the child page's Load event handler write the following server-side code.

protected void Page_Load(object sender, EventArgs e)

{

    const string cRefreshParent = "<script language='javascript'>" +

    "  window.opener.document.forms(0).submit();" + "</script>";

    const string cRefreshParentKey = "RefreshParentKey";

    if (!this.Page.ClientScript.IsClientScriptBlockRegistered(cRefreshParentKey))

    {

        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),

        cRefreshParentKey, cRefreshParent);

    }

}


I guess that this solution is simple but very useful if you want to create Web applications by the popup windows.