Search code examples
javascriptasp.netascx

How can I reach an aspx page from another in code behind?


I have two web page and one user control pages in my .aspx project.

Main.aspx

Controls/MiddlePage.ascx

FileUpload.aspx

I click a javascript link to open a popup window (FileUpload.aspx) in Controls/MiddlePage.ascx (embedded in Main.aspx) for loading an image. When an image has been uploaded successfully, I want to close popup automatically and want to carry image details to Main.aspx page automatically. How can I do this?

Can you help me please?

Thank you.


Solution

  • In the popup in the postback after the file has been successfully uploaded render the following js code that should close it

    <body onload="window.close();">
    

    If you want to return a value to the parent page, you can add an input control that can be hidden and set its value from popup before it will be closed

    <script type="text/javascript">
    function closeme()
    {
        window.opener.document.getElementById("InputImageName").value="<%#ImageName%>";
        window.close();
    }
    </script>
    ...
    <body onload="closeme();">
    

    where InputImageName is a control on parent page

    <input type="text" id="InputImageName" />
    

    and ImageName is a server variable that you define in the code behind during upload.

    UPDATE

    To refresh parent window, you can use reload() or specify new url for window.opener.location. Example:

    window.opener.location.reload();
    window.opener.document.location.href = url;
    

    See Open popup and refresh parent page on close popup