locked
After downloading file the loading screen it does not close ASP NET C# RRS feed

  • Question

  • User-1453200658 posted

    Hi,

    I have add a Button in GridView

    <asp:ScriptManager runat="server" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Name" />
            <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:HiddenField ID="hfCountry" runat="server" Value='<%# Eval("Country") %>' />
                    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
                        <ProgressTemplate>
                            <div class="modal">
                                <div class="center">
                                    <img alt="" src="progress.gif" />
                                </div>
                            </div>
                        </ProgressTemplate>
                    </asp:UpdateProgress>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="Link" EventName="Click" />
                        </Triggers>
                        <ContentTemplate>
                            <asp:Button ID="Link" runat="server" Text="Download" OnClick="OnDownload" CssClass="buttonLink" />
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
            font-family: Arial;
        }
     
        .modal {
            position: fixed;
            left: 0;
            z-index: 999;
            height: 100%;
            width: 100%;
            top: 0;
            background-color: Black;
            filter: alpha(opacity=60);
            opacity: 0.6;
            -moz-opacity: 0.8;
        }
     
        .center {
            z-index: 1000;
            margin: 30px auto;
            padding: 10px;
            width: 130px;
            background-color: White;
            border-radius: 10px;
            filter: alpha(opacity=100);
            opacity: 1;
            -moz-opacity: 1;
        }
     
            .center img {
                height: 128px;
                width: 128px;
            }
     
        .buttonLink {
            background: none !important;
            border: none;
            padding: 0 !important;
            font-family: arial, sans-serif;
            color: #069;
            text-decoration: underline;
            cursor: pointer;
        }
    </style>
    

    On the OnDownload I set that will navigate to the next form in the website for export xls file

    protected void OnDownload(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GridViewRow row = btn.NamingContainer as GridViewRow;
     
        string ese = (row.FindControl("hfCountry") as HiddenField).Value;
     
        if (!string.IsNullOrEmpty(ddl.SelectedValue))
        {
            Response.Redirect("xls.aspx?e=" + ese.ToString() + "&a=" + ddl.SelectedValue);
        }
        else
        {
            Response.Redirect("xls.aspx?e=" + ese.ToString());
        }
    }
    

    With this code my problem is after downloading the excel file from the page xls.aspx my loading screen it does not close and remains fixed on the browser...

    How to do resolve this?

    Thanks in advance for any help

    Saturday, February 20, 2021 11:54 AM

Answers

  • User-939850651 posted

    Hi Edward Sheriff Curtis,

    With this code my problem is after downloading the excel file from the page xls.aspx my loading screen it does not close and remains fixed on the browser...

    You seem to want the page to automatically close the page after the download file response is complete?

    If this is the case, I think you can try it like this:

    Response.ContentType = "xxx/xxx";
                Response.AppendHeader("Content-Disposition", "attachment; filename=xxx.xxx");
                Response.TransmitFile(filePath);
                Response.End();
                System.Threading.Thread.Sleep(2000);
                //ClientScript.RegisterClientScriptBlock(Page.GetType(), "script", "window.close();", true);
                Response.Write("<script language=javascript>this.window.opener = null;window.open('','_self'); window.close();   </script>");

    But in chrome browser, you can't close the page by script, you can only do it manually.

    The code works on IE and Edge, or you can choose to return to the previous page after the download is complete.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2021 9:39 AM