简体   繁体   English

将gridview导出到Excel后,页面加载未结束

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

I am trying to export gridview data to the excel. 我正在尝试将gridview数据导出到Excel。 Everything is alright for exporting but after that page load is not ending. 一切都可以导出,但是在那之后页面加载没有结束。

This is the script for "Please wait" in the master page (It shows a gif and says "Please wait while page is loading") : 这是母版页中“请稍候”的脚本(显示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>

This is the class where I do export : 这是我要导出的课程:

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();

        }
    }

And this is where I call the method in the master page : 这就是我在母版页中调用方法的地方:

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

This is because there can only be on response from the server side. 这是因为只能从服务器端进行响应。 In you case, that response is in a form of an excel file. 在您的情况下,该响应采用excel文件的形式。 One way to get around this by setting a timeout in your JavaScript with the code to remove the "Please wait" gif. 解决此问题的一种方法是在JavaScript中使用代码设置超时,以删除“请稍候” gif。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM