<p>我在做将GridView导出为Excel的功能时,碰到这个错误:<span class="Apple-style-span" style="font-family: Simsun; line-height: 18px; ">

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

我百思不得其解,因为我的GridView是放在一个有runat="server"的form标签中的呀!

后来找到解决办法,汗!看来微软做的软件,也会有一些不可思议的小Bug。

原文地址:http://m.cnblogs.com/28987/1320155.html


Vs2003中我們常用DataGrid導出到Excel的方法如下:

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "BIG5";
        string fileName = "Overview";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("BIG5");
        Response.ContentType = "application/ms-excel";//

        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-TW", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();

用以上方法在VS2005的项目时却报如下错误:

 

Server Error in '/DataMeasure' Application.

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

Source Error: 

Line 244: System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); Line 245: System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); Line 246: GridView1.RenderControl(oHtmlTextWriter); Line 247: Response.Write(oStringWriter.ToString()); Line 248: Response.End();

Source File: d:"SourceCode"DataMeasure"Overview"DataOverview.aspx.cs    Line: 246  

 

有两种解决方法:

一、在原来代码的基础上更改

导出代码不变

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "BIG5";
        string fileName = "Overview";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("BIG5");
        Response.ContentType = "application/ms-excel";//

        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-TW", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();

但是要添加如下代码:

 public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

要注意的是中间的代码行要注释掉,这就是忽略检查在RENDER的时候判断是否在RUNAT=SERVER中

二、另一种方法

这种方法是新建一个FORM,将GRIDVIEW放入这个FORM中。

 

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        Page page = new Page();
        HtmlForm form = new HtmlForm();
        GridView1.EnableViewState = false;
        page.DesignerInitialize();

        form.Controls.Add(GridView1);
        page.Controls.Add(form);

        page.RenderControl(htw);
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment;filename=Overview.xls");
        Response.Charset = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.Default;
        Response.Write(sb.ToString());
        Response.End();

 

以上两种方法都可以在VS2005中实现GridView导出到Office(Excel+Word)中.