Solved

How to refresh parent window when child window in closed?

Posted on 2011-02-14
12
1,138 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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