4

Im trying to use an asp:progress so when I click on my asp:button it calls an ashx file which write a CSV response.

So far when I click on the button, the animation shows properly and the download start. However I can't seem to stop the animation when the file start downloading (when I get the response from the ashx file).

Here is the aspx :

<asp:Content ID="Content2" ContentPlaceHolderID="MainPlaceHolder" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <p>Download a CSV file</p>
            <asp:Button ID="Button2" runat="server" Text="Download" 
            CausesValidation="False" onclick="Button2_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>

    <asp:UpdateProgress ID="UpdateProgress1"  AssociatedUpdatePanelID="UpdatePanel1" runat="server">
        <ProgressTemplate>
            <div id="AlertDiv" style="background:White">
                <asp:Image ID="imgLoading" runat="server" ImageUrl="css/smoothness/images/animated-overlay.gif" Width="34px" />Loading...
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>
</asp:Content>

Here is the Button2_click function :

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
        Response.Redirect("ProductsHierarchyDownload.ashx");
    }
    catch (Exception ex)
    {
      //log
    }
}

And here is the ProcessRequest function from the ashx file :

public void ProcessRequest(HttpContext context)
{
    string attachment = String.Format("attachment; filename=Hierarchie des produits au {0}.csv", DateTime.Today.ToShortDateString());
    context.Response.AddHeader("content-disposition", attachment);
    context.Response.ContentType = "text/csv";
    context.Response.AddHeader("Pragma", "public");
    context.Response.ContentEncoding = Encoding.GetEncoding(1252);
    context.Response.Write(DemandeTarifImageBLL.DataTableToCSVString());
}

I've tried to use javascript and the property endRequest, beginRequest and initializeRequest event of the PageRequestManager Class (I found some conde snippet here) but nothing has worked for me so far.

How do I force the animation to stop when the download starts ?

WizLiz
  • 2,068
  • 5
  • 25
  • 39

3 Answers3

1

Although I was not able to try and see if its working but by my previous experience I can suggest the following. Try changing your code as follows:

public void ProcessRequest(HttpContext context)
{
    string attachment = String.Format("attachment; filename=Hierarchie des produits au {0}.csv",        
    DateTime.Today.ToShortDateString());
    context.Response.AddHeader("content-disposition", attachment);
    context.Response.ContentType = "text/csv";
    context.Response.AddHeader("Pragma", "public");
    context.Response.ContentEncoding = Encoding.GetEncoding(1252);
    context.Response.Write(DemandeTarifImageBLL.DataTableToCSVString());
    context.Response.Flush();
    context.Response.End();
}

Adding Flush and End clears the buffer and marks the response end respectively. This way the client would reach a definite conclusion that the Request has been completely processed.

Also you can control the update progress with the client side script, here is a sample code:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(EndRequest);

function EndRequest(sender, args) {
            if (postBackElement.id == 'Panel1Trigger') {
                $get('UpdateProgress1').style.display = 'none';
            }
        }

The above js code adds an handler for update panels end request event and on the function closes the Update progress.

Try putting the contents of you ProcessRequest function of your handeler to the button click event, and do remember to add Flush() and End function for the response.

EDIT 1:

On looking further in stack overflow I found the following link with the issue similar to yours, and this confirmed my assumption that you can not have a redirect inside an update panel:

asp:UpdateProgress never goes away after clicking on downloadable content inside a repeater

Hope this helps.

Community
  • 1
  • 1
Shashank Chaturvedi
  • 2,756
  • 19
  • 29
  • Unfortunately, this doesnt change the outcome. Animation begins > download begins > animation remains in place after download complete – WizLiz Feb 24 '14 at 12:21
  • I have updated my answer, I think this will address your issue. – Shashank Chaturvedi Feb 24 '14 at 13:10
  • That's one of the thing I've tried but I dont understand why the EndRequest never seems to be called (I put an alert in it which never poped). I feel like the response.redirect end the execution of the javascript on the client side and I can't manage to overcome this issue – WizLiz Feb 24 '14 at 13:26
  • You should try putting the contents of you ProcessRequest function of your handeler to the button click event. And do remember to add Flush() and End function for the response. – Shashank Chaturvedi Feb 24 '14 at 13:33
1

Try redirect to ashx from javascript.

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "downloadFileExport", "javascript:window.location ='" + ResolveUrl(downloadUrl) + "';", true)

This was solved my issue too.

HATCHA
  • 600
  • 1
  • 8
  • 15
0

In my ASP.Net Web Forms application the progress text kept showing even after the process was done. I was able to fix that in Dev environment by increasing the timeout in the ScriptManager. Example below.

enter image description here

Stand4Unborn
  • 351
  • 1
  • 2
  • 9