我正在尝试导出excel一个简单的gridview,当我试图将gridview控件渲染到HtmlTextWriter时,我得到一个空字符串,同时当我在做一个quickwatch到gridview时它会显示我出现的行。
我正在使用的代码,
protected void btnExportAuto_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvNoDataFound.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//
}
ASPX:
<asp:GridView ID="gvNoDataFound" runat="server" Visible="False" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField HeaderText="ApplicationID" HeaderStyle-Width="150px" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="lblApplicationID2" runat="server" Text='<%# Bind("ApplicationID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="gridHeader" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ApplicationTransactionID" HeaderStyle-Width="150px" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="lblApplicationTransactionID2" runat="server" Text='<%# Bind("ApplicationTransactionID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="gridHeader" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment" HeaderStyle-Width="150px" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="lblComment2" runat="server" Text='<%# Bind("Comment") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="gridHeader" />
</asp:TemplateField>
</Columns>
</asp:GridView>
不确定我是否遗漏了任何愚蠢的东西,我把所有东西都放在了runat =“server”中,仍然没有运气
答案 0 :(得分:0)
RenderControl
的输出为空,因为GridView的Visible="false"
。您可以在呈现内容之前使其可见:
gvNoDataFound.Visible = true;
gvNoDataFound.RenderControl(htmlWrite);
gvNoDataFound.Visible = false;