Having problems getting window.opener.reload() to work in an ascx file.

Gary Globus 6 Reputation points
2022-06-27T20:23:20.347+00:00

I have an ascx control file which calla an aspx page. While in the aspx page I want to force the equivalent of an f5 reload on the ascx page so it will update the data contained on the page. I have attempted to do this in javaScript using a number of suggested ways. Some of the code variations I have tried are listed below. I am using asp frame 4.5 in Microsoft Studio 2019. The following are the attempts in the aspx page to run this code:

code

    function refreshAndClose1() { //This was the first thing I tried (in orig   
                                                    //IE11 version  
        opener.location.reload();  
        window.close();  
    }  
    async function refreshAndClose() {  
        function doit() {  
            if (window.opener && !window.opener.closed) {  
                window.opener.location.reload();  
            }  
        }  
        window.close();  
        setTimeout(doit, 100);  
    }  
    function surrogateUpDateFunc() {  
        alert('We are inside the aspx function');  
        window.opener.document.Form1.getElementById('btnRefresh').click();  
        //function reloadwin() {  
        //    window.open('', '_self', '');  
        //    self.close();  
        // }  
        //setTimeout(reloadwin, 1000);  
        self.close();  
        //alert('We are about to attempt unload of opener!');  
        //window.opener.reload();  
        //window.opener.document.location.reload();  
    }  

End of Code.

None of these have worked. In the last one I put a button in the ascx page. That button when you click on it in the ascx page a refresh of the data in the ascx page is refreshed manually. When I attempt to do that programatically from the aspx page nothing happens. That is the "'btnRefresh" ID that I am attempting to click on. This code was written by someone else. He is no longer with us but I am trying to understand why this will not work. I did notice that he makes use of a form of what he calls modal mode where he is trying to emulate in Chrome what was done before in IE11. I am wondering if there in lies the problem. I have in all other parts of this program used the window.opener.reload() with no problem. Is there something peculiar to ascx pages that I am not aware of that makes this not work or perhaps there is another way to do this. The THIS that I need to do is from the child process which is an aspx page I need to force the calling program an ascx page to refresh itself.

Developer technologies | ASP.NET | Other
{count} vote

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-06-28T08:45:00.17+00:00

    Hi @Gary Globus ,
    According to your description you want to achieve refresh ascx page. I think you can try the following two methods.
    1.Use an AJAX UpdatePanel with a timer to automatically refresh data.

    <asp:UpdatePanel runat="server" D="UpdatePanel1">  
        <ContentTemplate>  
            <asp:Timer runat="server" ID="clock" OnTick="clock_Tick" Interval="1000" Enabled="true"></asp:Timer>  
            <uc1:ProgressBar runat="server" ID="ProgressBar" />  
        </ContentTemplate>  
        <Triggers  >  
            <asp:AsyncPostBackTrigger ControlID="clock" EventName="Tick" />  
        </Triggers>  
    </asp:UpdatePanel>  
    protected void clock_Tick(object sender, EventArgs e)  
    {  
       UpdatePanel1.Update();  
    }  
    

    2.If you're creating a user control that's being built based on data saved. What you can do is create a method that does that building and then call it within the page and user control:
    UserControl:

    protected Page_Load(object sender, EventArgs e)  
    {  
        BuildControlBasedOnData();  
    }  
    
    public BuildControlBasedOnData()  
    {  
        // Build the user control based on saved data  
    }   
    

    Calling Page:

    Button_Click(object sender, EventArgs e)  
    {  
        UserControl1.BuildControlBasedOnData();      
    }  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-06-28T15:25:52.38+00:00

    if the opened page does a post back or navigation, opener is no longer valid (due to browser security). be sure the page only calls Ajax, no form posts.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.