代码        protected void Page_Load(object sender, EventArgs e)
        {
            //页面数据生成前在前台显示等待页面
            Response.Write("<div id='mydiv' >");
            Response.Write("_");
            Response.Write("</div>");
            Response.Write("<script>mydiv.innerText = '';</script>");
            Response.Write("<script language=javascript>;");
            Response.Write("var dots = 0;var dotmax = 10;function ShowWait()");
            Response.Write("{var output; output = '正在装载页面';dots++;if(dots>=dotmax)dots=1;");
            Response.Write("for(var x = 0;x < dots;x++){output += '·';}}");
            Response.Write("mydiv.innerHTML = '<img src=loading.gif>';");
            Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible'; ");
            Response.Write("window.setInterval('ShowWait()',1000);}");
            Response.Write("function HideWait(){mydiv.style.visibility = 'hidden';");
            Response.Write("window.clearInterval();}");
            Response.Write("StartShowWait();</script>");
            Response.Flush();
 //页面数据生成前在前台显示等待页面            if (Session.Contents["user"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (!Page.IsPostBack)
            {
               
                this.fund_account.Text = Session.Contents["fund_account"].ToString();
                this.Text_start.Text = Session.Contents["Text_start"].ToString();
                this.Text_end.Text = Session.Contents["Text_end"].ToString();
                initData();
             }
        }//导出excel代码
 protected void CreateExcel(GridView gv)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GBK";
            Response.AppendHeader("Content-Disposition", "attachment;filename=Data.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GBK");
            Response.ContentType = "application/ms-excel";
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            gv.RenderControl(oHtmlTextWriter);            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }
按钮调用
  protected void Button2_Click(object sender, EventArgs e)
        {
            CreateExcel(GV_cover);
           
        }
代码运行正常,在数据页面生成前有等待图 等页面数据加载完后生成数据页面,但是一运行导出代码 就弹出错误“服务器无法在发送 HTTP 标头之后设置内容类型”如果把
 //页面数据生成前在前台显示等待页面
代码注释的话  导出功能恢复正常请问怎么有什么好的方案可以解决吗?

解决方案 »

  1.   

      Response.Write("<div id='mydiv' >");
      Response.Write("_");
      Response.Write("</div>");
      Response.Write("<script>mydiv.innerText = '';</script>");
      Response.Write("<script language=javascript>;");
      Response.Write("var dots = 0;var dotmax = 10;function ShowWait()");
      Response.Write("{var output; output = '正在装载页面';dots++;if(dots>=dotmax)dots=1;");
      Response.Write("for(var x = 0;x < dots;x++){output += '·';}}");
      Response.Write("mydiv.innerHTML = '<img src=loading.gif>';");
      Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible'; ");
      Response.Write("window.setInterval('ShowWait()',1000);}");
      Response.Write("function HideWait(){mydiv.style.visibility = 'hidden';");
      Response.Write("window.clearInterval();}");
      Response.Write("StartShowWait();</script>");
      Response.Flush();
    这段放在  Response.Write("<div id='mydiv' >");
      Response.Write("_");
      Response.Write("</div>");
      Response.Write("<script>mydiv.innerText = '';</script>");
      Response.Write("<script language=javascript>;");
      Response.Write("var dots = 0;var dotmax = 10;function ShowWait()");
      Response.Write("{var output; output = '正在装载页面';dots++;if(dots>=dotmax)dots=1;");
      Response.Write("for(var x = 0;x < dots;x++){output += '·';}}");
      Response.Write("mydiv.innerHTML = '<img src=loading.gif>';");
      Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible'; ");
      Response.Write("window.setInterval('ShowWait()',1000);}");
      Response.Write("function HideWait(){mydiv.style.visibility = 'hidden';");
      Response.Write("window.clearInterval();}");
      Response.Write("StartShowWait();</script>");
      Response.Flush();
    这段需要放在if (!Page.IsPostBack)里  就能解决