Solved

How to refresh parent window when child window in closed?

Posted on 2011-02-14
12
1,103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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