Click here to Skip to main content
15,076,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can export from a gridview when no item templates placed inside gridview But now I need to add a linkbutton as Item Template ,Then blank file is exporting.

source code as below

<asp:GridView ID="grdBorrowerMaster" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        CssClass="GridFont1" OnPageIndexChanging="grdBorrowerMaster_PageIndexChanging"
        OnSelectedIndexChanged="grdBorrowerMaster_SelectedIndexChanged" PageSize="20"
            <asp:TemplateField HeaderText="Borrower Name" ShowHeader="False">
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
                        Text='<%# Bind("NameAll") %>'></asp:LinkButton>
            <asp:TemplateField HeaderText="Applied Date">
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CurrDate") %>'></asp:TextBox>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("CurrDate","{0:MM/dd/yyyy}") %>'></asp:Label>
                    <asp:Label ID="lblBorrowerId" runat="server" Text='<%# Bind("BorrowerId") %>' Visible="False"></asp:Label>
            <asp:BoundField DataField="NameAll" HeaderText="Borrower Name" Visible="False" />
            <asp:BoundField DataField="BorkerName" HeaderText="Broker Name" />
            <asp:BoundField DataField="STATUS" HeaderText="Status" SortExpression="STATUS" />
            <asp:BoundField DataField="Processor" HeaderText="Processor" />

How can avoid this issue

Exporting code is below

protected void butexport_Click1(object sender, EventArgs e)
            Response.AddHeader("content-disposition", "attachment;filename=PipelineReport.xls");
            Response.Charset = "";
            // If you want the option to open the Excel file without saving than
            // comment out the line below
            // Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            StringWriter stringWrite = new System.IO.StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            grdBorrowerMaster .RenderControl (htmlWrite );
        catch (Exception ex)
            lblError.Visible = true;
            lblError.Text = ex.Message;


 public override void VerifyRenderingInServerForm(Control control)  
      //Confirms that an HtmlForm control is rendered for the specified ASP.NET   
       //server control at run time.  
Updated 27-Jan-11 20:12pm
Sunasara Imdadhusen 28-Jan-11 1:54am
Where is the code for exporting logic?

1 solution

Are you using the HtmlTextWriter and RenderControl . I guess you cannot do it if u use the default RenderControl.
U might have to do the export manually or create another grid where u write the text instead of linkbuttons and export that one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900