• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2918
  • Last Modified:

how to close window after automatic download

We have a web application that generates a report in a popup window based on choices made in the main window. When the report is generated a download should automatically start. After the download, the popup window should close itself.

The automatic download is done by a download.aspx page with a call to Respose in the PageLoad. This works fine, except that all kinds of methods to close the window, don't work.

Everywhere I find that you should use window.close(); with a timer. However, after a Response.Redirect or Response.end no javascript is executed any more, so the window will not close.

In both the generate.aspx and download.aspx pages I have a hidden field which is set to "close" after the generation is done and a timer to check its contents and close if it says close.
But it doesn't work.

I'm probably missing a point somewhere, but haven't found lots of similar questions but no working answers...

Hope someone can help.
in the popup window generate.aspx.vb:
hfld_1.Value = "close"
Response.Redirect("download.aspx?" & _qryString, False)

in download.aspx.vb:
Response.ContentType = "application/pdf"
        Response.AppendHeader("Content-Disposition", "attachment; filename=" & _strFileName)
        Response.TransmitFile(_strFilePath)
        Response.End()

Open in new window

0
DBIC
Asked:
DBIC
  • 9
  • 4
  • 2
3 Solutions
 
AlsaeedCommented:
Response.Redirect will flush all values such as hfld_1.Value = "close".
You may create an IFRAME within the generate.aspx page and set the iframe source to download.aspx so that you can control the popup window from outside the iframe.
0
 
rajapandian_81Commented:
Try below code to close window

Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=JavaScript>window.close();</script>");

Open in new window

0
 
DBICAuthor Commented:
@rajapandian_81:

I already tried that, but it won't work...
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DBICAuthor Commented:
Ok, I will try to use the IFrame, but how can I control when the IFrame loads the download.aspx (after the report is generated), and how can I add the querystring which is generated in the VB code?
0
 
DBICAuthor Commented:
Thanks! This is a very good explanation, exactly what I needed to use the IFrame. When I'm back in the office on Tuesday, I will work on this. This kind of information is very hard to find between all the results Google gives...
0
 
DBICAuthor Commented:
I can now use the IFrame, but unfortunately the popup window still doesn't close...

Anyone having ideas what I'm doing wrong?

My code:
in generator.aspx.vb:
 Create_Report()
 Send_Report()
 hfld_1.Value = "close"

in Sub Send_Report:
 _src = String.Concat("download.aspx?strID=", _strID, etc.)
 iframe_download.Visible = True
 iframe_download.Attributes("src") = _src

in generator.aspx:
    function InitializeTimer()
    {
        var _hfld = document.getElementById('hfld_1');
        var _value = _hfld.value;

        if (_value == 'close')
        {
            window.close();
        }
    }

in download.aspx.vb:
 Response.ContentType = "application/pdf"
 Response.AppendHeader("Content-Disposition", "attachment; filename=" & _strFileName)
 Response.TransmitFile(_strFilePath)
 Response.End()

Open in new window

0
 
rajapandian_81Commented:
When and how you are calling the function InitializeTimer() ?
Are you getting any javascript error ?
0
 
DBICAuthor Commented:
There is no javascript error, but the animated image stops. The function is called in de body onload:

    <body onload="InitializeTimer()" >

When an email with the report is sent to the user, the popup window disappears, but not when the report is offered for download (option on the form where the report is requested).

All javascript functions on generator.aspx (download.aspx contains no javascript):
// Script to generate an automatic postBack to the server
var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer()
{
    var _hfld = document.getElementById('hfld_1');
    var _value = _hfld.value;
    if (_value == 'close')
    {
        window.close();
    }
    else
    {
        // Set the length of the timer,
        // in seconds. Your choise
        secs = 2

        StopTheClock()
        StartTheTimer()
    }
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()

        //Generate a Postback to the server, after Postback the report is generated in VB code
        document.forms[0].submit()  
        
        //alert('hallo')
       
        setTimeout("UpdateImg('load_img','../App_Images/progress_bar.gif')",500)
       
    }
    else
    {
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function UpdateImg(ctrl,imgsrc) 
{
 var img = document.getElementById(ctrl);
 img.src = imgsrc;
}
//-->
</script>

<head id="Head1" runat="server">
    <title>Genereren Rapport</title>
</head>
<body onload="InitializeTimer()" >

Open in new window

0
 
rajapandian_81Commented:
Don't call function InitializeTimer() in body onload. Call using setInterval() like below.
Inside script tag add below code.
setInterval ( "InitializeTimer()", 5000 );

Open in new window

0
 
DBICAuthor Commented:
That helped!
But not all reports are finished in 5 seconds, so I'll cleanup the code and will take care the the postback is only executed once...
Then I'll post the working solution, so others can benefit from it.
0
 
DBICAuthor Commented:
It works!

Thanks for pointing me out. I've cleaned up the code, removed all unnecessary loops and now have a quite simple working result, see below.

The result is a popup window, that generates the report, then presents the result for download and closes itself.

The code:


generator.aspx:
<script language="JavaScript" type="text/javascript" >

// After 1 sec do the postback and start animated gif in body onload
//setTimeout ("StartGenerate()",1000);
// Check every 5 secs if the popup window should be closed
setInterval ( "ClosePopup()", 5000 );


function StartGenerate()
{
    //alert('do PostBack')
    //Generate a Postback to the server
    document.forms[0].submit()  
                
    setTimeout("UpdateImg('load_img','../App_Images/progress_bar.gif')",500)
}

function UpdateImg(ctrl,imgsrc) 
{
 var img = document.getElementById(ctrl);
 img.src = imgsrc;
}

function ClosePopup()
{
    var _hfld = document.getElementById('hfld_1');
    var _value = _hfld.value;
    
    //alert(_value);
    if (_value == 'close')
    {
        window.close();
    }
}

</script>

<head id="Head1" runat="server">
    <title>Genereren Rapport</title>
</head>
<body onload="setTimeout ('StartGenerate()',1000);">
...
<asp:hiddenfield ID="hfld_1" runat="server" Value="open" />
...

in generator.aspx.vb:
 Create_Report()
 Send_Report()
 hfld_1.Value = "close"

in Sub Send_Report:
 _src = String.Concat("download.aspx?strID=", _strID, etc.)
 iframe_download.Visible = True
 iframe_download.Attributes("src") = _src

in download.aspx.vb:
 Response.ContentType = "application/pdf"
 Response.AppendHeader("Content-Disposition", "attachment; filename=" & _strFileName)
 Response.TransmitFile(_strFilePath)
 Response.End()

Open in new window

0
 
DBICAuthor Commented:
Both many thanks for pointing me (and probably lots of other people) to the solution.
0
 
DBICAuthor Commented:
Hmm, when testing in Firefox it appeared that Firefox constantly reloads the page, resulting in lots of open/save dialogs...

So I made a little adjustment. The function StartGenerate() now checks if hfld_1.value = 'open' and then does the postback, else nothing. In the code behind the value is set to 'running' when handling the postback, so there will only be 1 postback.

Also, IE waits till you have opened or saved the document before closing the popup. Firefox doesn't, it just closes the popup AND the open/save dialog. I haven't found a solution for that yet.
0
 
rajapandian_81Commented:
For me Firefox closes only popup, not the open/save dialog.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now