簡體   English   中英

將gridview導出到Excel后,頁面加載未結束

[英]Page Load is not ending after exporting gridview to Excel

我正在嘗試將gridview數據導出到Excel。 一切都可以導出,但是在那之后頁面加載沒有結束。

這是母版頁中“請稍候”的腳本(顯示gif並顯示“頁面正在加載,請稍候”):

<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>

這是我要導出的課程:

public static void Export(string fileName, GridView gv)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader(
        "content-disposition", string.Format("attachment; filename={0}", fileName));
    HttpContext.Current.Response.ContentType = "application/ms-excel";

    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            //  Create a form  to contain the grid
            Table table = new Table();

            //  add  the header row to the table
            if (gv.HeaderRow != null)
            {

                GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                table.Rows.Add(gv.HeaderRow);
                //color the header
                table.Rows[0].BackColor = gv.HeaderStyle.BackColor;
                table.Rows[0].ForeColor = gv.HeaderStyle.ForeColor;
            }

            //  add each of the data  rows to the table
            foreach (GridViewRow row in gv.Rows)
            {
                GridViewExportUtil.PrepareControlForExport(row);
                table.Rows.Add(row);
            }

            //  add the footer row to the table
            if (gv.FooterRow != null)
            {
                GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                table.Rows.Add(gv.FooterRow);
            }


            table.GridLines = gv.GridLines;
            //  render the table into the htmlwriter

            table.RenderControl(htw);
            //  render the htmlwriter into the response
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();

        }
    }

這就是我在母版頁中調用方法的地方:

    GridView gridview = (GridView)ContentPlaceHolder1.FindControl("gridview_sales");
    gridview.AllowPaging = false;
    gridview.DataBind();
    GridViewExportUtil.Export("SalesList.xls", gridview);

這是因為只能從服務器端進行響應。 在您的情況下,該響應采用excel文件的形式。 解決此問題的一種方法是在JavaScript中使用代碼設置超時,以刪除“請稍候” gif。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM