Click here to Skip to main content
16,022,352 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to hide progress bar after downloading the data into excel.

In run mode as follows

i have gridview data from database. i have one Button Export to excel when i click that button gridview data is downloading to excel.

for downloading gridview data to excel code as follows

C#
protected void btnExport_Click(object sender, EventArgs e)

       {
           System.Threading.Thread.Sleep(3000);

           string script = "$(document).ready(function () { $('[id*=btnExport]').click(); });";
           ClientScript.RegisterStartupScript(this.GetType(), "load", script, true);

           Response.ClearContent();
           Response.Buffer = true;
           Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Employee.xls"));

           Response.ContentType = "application/ms-excel";
           System.IO.StringWriter sw = new System.IO.StringWriter();
           HtmlTextWriter htw = new HtmlTextWriter(sw);

           gvEmpdetails.AllowPaging = false;
           BindGrid();
          
           //Applying stlye to gridview header cells
           for (int i = 0; i < gvEmpdetails.HeaderRow.Cells.Count; i++)
           {
               gvEmpdetails.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
           }
           gvEmpdetails.RenderControl(htw);
           Response.Write(sw.ToString());
           Response.End();

       }


Source code as follows

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        .modal
        {
            position: fixed;
            top: 0;
            left: 0;
            background-color: black;
            z-index: 99;
            opacity: 0.8;
            filter: alpha(opacity=80);
            -moz-opacity: 0.8;
            min-height: 100%;
            width: 100%;
        }
        .loading
        {
            font-family: Arial;
            font-size: 10pt;
            border: 5px solid #67CFF5;
            width: 200px;
            height: 100px;
            display: none;
            position: fixed;
            background-color: White;
            z-index: 999;
        }
    </style>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function ShowProgress() {
        setTimeout(function () {
            var modal = $('<div />');
            modal.addClass("modal");
            $('body').append(modal);
            var loading = $(".loading");
            loading.show();
            var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
            var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
            loading.css({ top: top, left: left });
        }, 200);
    }
    $('form').live("submit", function () {
        ShowProgress();
    });
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div align ="Center">
    
        <asp:Label ID="label" Text = "Proof of conecpt" Font-Bold ="True" forecolor ="Blue"  runat ="server"></asp:Label>
    </div>
        <asp:GridView ID="gvEmpdetails" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>

     <div class="loading" align="center">
        Loading. Please wait.<br />
        <br />
        <img src="loader.gif" alt="" />
    </div>

        <div>
          <asp:Button ID="btnExport" runat="server" Text="Export to Excel" OnClick="btnExport_Click" />
          </div>
    </form>
</body>
</html>


When i click the Exporttoexcel progress bar displaying after that data is displaying into excel.

Then i close the excel. and in the background progress bar is running.

i want to hide that progress bar.

for that how can do.
Posted
Updated 2-May-16 23:02pm
v2
Comments
Can't you hide after the operation?

Try this

Create an iframe in form1
ASP.NET
<form id="form1" runat="server">
         <iframe id="iframe1" src="" runat="server" style="visibility:hidden"></iframe></form>

Change your button click event as below
C#
protected void btnExport_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(3000);

            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gvEmpdetails.AllowPaging = false;
            BindGrid(); 
            //Applying stlye to gridview header cells
            for (int i = 0; i < gvEmpdetails.HeaderRow.Cells.Count; i++) 
                gvEmpdetails.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
            
            gvEmpdetails.RenderControl(htw);
          
            Session["ResponseData"] = sw.ToString();
            iframe.Attributes.Add("src","Download.aspx");
             
        }


Add this script

JavaScript
<script type="text/javascript">
        $(function () {
            var loading = $(".loading");
            loading.hide();
        });
    </script>

--------------------------------------------------------
Create a new page, name should be Download.aspx

C#
public partial class Download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Employee.xls"));
            Response.ContentType = "application/ms-excel";
            string responseString = Session["ResponseData"] + "";
            Response.Write(responseString);
            Response.End();
        }
    }
 
Share this answer
 
worked for me this solution
adding iframe on the page where download button is present inside update panel with trigger

<asp:postbacktrigger controlid="btnDownload">



In the code beind

Session(DownloadFilePath) = filePath<br />
iframe1.Attributes.Add("src", "DownloadFile.aspx?filePath=" + DownloadFilePath)<br />


in page_load of DownloadFile.aspx

Dim filePath As String = Session(Request.QueryString("filePath"))<br />
               If filePath IsNot Nothing AndAlso File.Exists(filePath) Then<br />
                   Dim _file As FileInfo = New FileInfo(filePath)<br />
                   Response.BufferOutput = True<br />
                   Response.AddHeader("Content-Length", _file.Length)<br />
                   Response.AddHeader("content-disposition", "attachment; filename=" & _file.Name)<br />
                   Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"<br />
                   Response.TransmitFile(filePath)<br />
                   HttpContext.Current.Response.Flush()<br />
                   File.Delete(filePath)<br />
                   Try<br />
                       Response.End()<br />
                   Catch ex As Exception<br />
                       'Prevents any other content from being sent to the browser<br />
                       HttpContext.Current.Response.SuppressContent = True<br />
                       'Directs the thread to finish, bypassing additional processing<br />
                       HttpContext.Current.ApplicationInstance.CompleteRequest()<br />
                   End Try<br />
               End If
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900