Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
asp.net(C#) To Excel时出现这种错误!!
C# Code
/// <summary>
/// To Excel
/// </summary>
/// <param name="ctl">例如控件 GridView</param>
private void ToExcel(Control ctl)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
然后才发现需要重写一个方法,加下面一段代码就可以搞定了.
public override void VerifyRenderingInServerForm(Control control)
{
}