我在网页中的WebUserControl中有一个GridView,它使用母版页,我需要将网格数据导出为excel和pdf。 我找到了这段代码:
Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
((GridView)Hosts.FindControl("hostsGrid")).RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
但是我收到了这个错误:
Control 'CPH_Body_Hosts_hostsGrid' of type 'GridView' must be placed inside
a form tag with runat=server.
即使我在Master页面上有一个表单runat服务器。
答案 0 :(得分:0)
我找到了2个解决这个问题的方法: