zoukankan      html  css  js  c++  java
  • 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。

    错误的写法:
     if (this.GridView1.Rows.Count > 0)
            {
                string style = @"<style> .text { mso-number-format:@; } </script> ";
                string exportfile = "supp" + DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "");
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=" + exportfile + ".xls");
     
                Response.ContentType = "application/excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
     
                GridView1.RenderControl(htw);
                Response.Write(style);
                Response.Write("<META http-equiv=Content-Type content='text/html; charset=utf-8'>" + sw.ToString());
                Response.End();
            }
            else
            {
                ShowMessageBox("无数据导出!");
            }
    此时会报出错误提示:

    类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。

    解决导出Excel问题  if (this.GridView1.Rows.Count > 0)
            {
                string style = @"<style> .text { mso-number-format:@; } </script> ";
                string exportfile = HttpUtility.UrlEncode("考核数据分析", System.Text.Encoding.UTF8) + System.DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "") + System.DateTime.Now.ToString("hh-mm-ss").Replace("-", "");
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=" + exportfile + ".xls");
                Response.ContentType = "application/excel";
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                GridView1.Columns[GridView1.Columns.Count - 1].Visible = false;
                GridView1.RenderControl(htw);
                Response.Write(style);
                Response.Write("<META http-equiv=Content-Type content='text/html; charset=gb2312'>" + sw.ToString());
                Response.End();
            }  

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

  • 相关阅读:
    20160130.CCPP体系详解(0009天)
    20160129.CCPP体系详解(0008天)
    20160128.CCPP体系详解(0007天)
    20160127.CCPP体系详解(0006天)
    20160126.CCPP体系详解(0005天)
    程序员_你的“强迫症”有哪些?
    天天写业务代码_如何成为技术大牛?
    阿里云全球首批MVP李文毅专访-一个“改邪归正”的90后
    【毕业季】穿越回毕业前一年_这次你会怎么选
    恢复Hyper-V虚拟机丢失的数据文件过程
  • 原文地址:https://www.cnblogs.com/liouxing199/p/5535011.html
Copyright © 2011-2022 走看看