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

    }

  • 相关阅读:
    《网络攻防实践》6.0
    《网络攻防实践》5.0
    Docker 本地镜像发布到阿里云(完结篇)
    Vue 实战-9 Vue公共js功能函数的封装和使用
    Vue 实战-8 单独运行测试.js文件
    Docker 常用安装
    DockerFile 解析及案例
    Docker 容器数据卷
    Docker 镜像原理
    多字段模糊匹配 -->搜索功能(mysql原生语句实现)
  • 原文地址:https://www.cnblogs.com/liouxing199/p/5535011.html
Copyright © 2011-2022 走看看