简体   繁体   中英

GridView RenderControl Not working?

I am trying to export to excel a simple gridview , When I am trying to render gridview control to a HtmlTextWriter I am getting an empty string , at the same time when I am doing a quickwatch to gridview it's show me rows present .

Code I am using ,

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>

not sure if I am missing anything silly , I put everything in runat="server" , still no luck

The output of RenderControl is empty because Visible="false" for the GridView. You can make it visible before rendering the content:

gvNoDataFound.Visible = true;
gvNoDataFound.RenderControl(htmlWrite);
gvNoDataFound.Visible = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM