Solved

How to refresh parent window when child window in closed?

Posted on 2011-02-14
12
1,034 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 99
Not showing JavaScript in the list 5 35
Convert an ASPX page into PDF 7 21
Web Form VB.Net  import CSV 4 17
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

12 Experts available now in Live!

Get 1:1 Help Now