Solved

How to refresh parent window when child window in closed?

Posted on 2011-02-14
12
1,014 Views
Last Modified: 2012-05-11
I opening my pop up in parent window on link button click

PARENT window code to open child window

Public Shared Sub OpenPopUp(ByVal response As HttpResponse, ByVal url As String, ByVal target As String, ByVal windowFeatures As String)
        If (String.IsNullOrEmpty(target) OrElse target.Equals("_self", StringComparison.OrdinalIgnoreCase)) AndAlso String.IsNullOrEmpty(windowFeatures) Then
            response.Redirect(url)
        Else
            Dim page As Page = CType(HttpContext.Current.Handler, Page)
            If page Is Nothing Then
                Throw New InvalidOperationException("Cannot redirect to new window outside Page context.")
            End If
            url = page.ResolveClientUrl(url)
            Dim script As String
            If (Not String.IsNullOrEmpty(windowFeatures)) Then
                script = "window.open(""{0}"", ""{1}"", ""{2}"");"
            Else
                script = "window.open(""{0}"", ""{1}"");"
            End If
            script = String.Format(script, url, target, windowFeatures)
            ScriptManager.RegisterStartupScript(page, GetType(Page), "Redirect", script, True)
        End If
    End Sub

Protected Sub lBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lBtn.Click
            OpenPopUp("Child.aspx", "recipients", "width=800,height=700,status=0,menubar=1,toolbar=0,location=0,scrollbars=0")
      End Sub

It is working perfectly fine.
I do not have any issue with this code. I would prefer to keep it this way.

CHILD window code
I am using ASP.Net button on the child window.Since I have to implement some functionality on code behind of this button.

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
       /// CODE
              Page.ClientScript.RegisterStartupScript(Me.GetType(), "close", "Done();", True)          
      End Sub

ASPX code

<script language="javascript" type="text/javascript">
        function Done() {
            window.close();
        }
        </script>
Now my question is how do I refresh my Parent window when my child window is closed?
0
Comment
Question by:AppDevs
  • 6
  • 5
12 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34888755
Before the window.close() function on your child page, you could add:

window.opener.location.reload(true);

or something similar. There are numerous ways to reload a page, as seen here: http://www.mediacollege.com/internet/javascript/page/reload.html
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34888756
This logic works for me.
This javascript code will load a popup and will check in intervals whether it is closed or not. Once found closed, it will refresh the parent page.
// Javascript Code

 <script type="text/javascript" language="javascript">
        var int;
       
        function redirect()
        {
            location.href='Parentpage.aspx';
        }
       
        function openWin(mypage, myname, w, h,scroll,resize)
            {
                int=self.setTimeout("checkWin()",1000);
                  var winl = (screen.width - w) / 2;
                  var wint = (screen.height -h) / 2;
                  winprops ='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resize+',status=1'
                  myWindow = window.open(mypage, myname, winprops)
                  if (parseInt(navigator.appVersion)>= 4)
                  { myWindow.window.focus(); }
            }

        function checkWin()
        {
            if (myWindow.closed)
            {
                redirect();
            }
            else {
               int=self.setTimeout("checkWin()",1000);
            }
        }

    </script>



// C# Code
 private void PopupTargetWindow(string PopupWindow)
    {
        string strjscript = "<script language='javascript'>";
        strjscript += "openWin('" + PopupWindow + "','','800','400','yes','no');";
        strjscript += "</script" + ">";
        RegisterStartupScript("Target", strjscript);

    }
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34888774
Another issues I faced someother approaches is that, when parent page is reloaded - the last function will be executed.

For eg:- Since the last action is opening popup, when close popu and reload parent page - it again loaded the same popup!

Anyway I finally acheived in this way.

Raj
0
 

Author Comment

by:AppDevs
ID: 34888995
RajkumarGS:
Child window is closing perfectly, but the data in the parent is not updated.
For example, I am updating Phone number in the child window, when the child window is closed, I want the phone number in the parent window to be updated. how do I do it?
0
 

Author Comment

by:AppDevs
ID: 34889006
and also I want to retain all the entries I made in the parent window, before I opened the child window.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34889142
When you close popup, is the parent window refreshes ?
If yes, check whether the code inside
If (!IsPostBack)
{
}
is executing ?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:AppDevs
ID: 34889355
It is going into the loop and reloading the page.
how do make it retain the previous selection and entries and display the updated information on the parent page.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34889501
Can you try the suggestion here
http://p2p.wrox.com/html-code-clinic/5034-pop-up-close-refresh-parent.html

Instead of this
  location.href='Parentpage.aspx';

The alternative solution, that I know - need some more effort. So first try this and let me know
0
 

Author Comment

by:AppDevs
ID: 34889710
I tried it, it is not working.
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 34889758
Alternative solution is - Store the values somewhere before load popup and when parent page is reloaded, you need to restore from that temporary storage. It could be using SessionState, QueryString, ViewState OR using temporary tables in database.

For this case, you need to modify this line in javascript, by adding some querystring value
location.href='Parentpage.aspx';
to
location.href='Parentpage.aspx?IsBack=1';
This is to identify whether the page is reloaded from popup.

If Page_Load, you need to check whether IsBack querystring has value=1. Then load values from the storage which you preferred.

In one of my very old project in ASP.Net, I done the same thing using QueryString. In datagrid there is one textbox for each row. I passed these textbox values as comma separated to popup as querystring and passed back.

Raj
0
 

Author Closing Comment

by:AppDevs
ID: 34891079
It is working, Thank you.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34893401
Glad I could help!

Which option you choosed?

Raj
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now