[英]How to export asp grid view inside a user control to excel?
我在网页的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.
即使我在“主页”上有一个表单运行服务器。
好吧,我找到了2个解决此问题的方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.