locked
Export gridview to PDF RRS feed

  • Question

  • User-2094959909 posted

    Hello everyone,

    i show my data in a gridview in an independent html page so i added a button a put on it the code to export to pdf 

         protected void Button1_Click(object sender, EventArgs e)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition",
                 "attachment;filename=GridViewExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                GridView1.AllowPaging = false;
                GridView1.DataBind();
                GridView1.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();
            }
    public override void VerifyRenderingInServerForm(Control control)
    {
    /*base.VerifyRenderingInServerForm(control);*/
    }

    so that when i started my page to test the button i had this error message and i don't know what is the problem

    Server error in the '/' application.

    Compilation error
    Description: An error occurred while compiling a resource required to respond to this request. Please see the error details below and edit your source code appropriately. 

    Compiler error message: CS1061: 'cosultprod_aspx' does not contain a definition for 'Button1_Click' and no extension method 'Button1_Click' accepting a first argument of type 'cosultprod_aspx' could be found reference?) 

    Source error:

     

    Ligne 79 : </asp:GridView>
    Ligne 80 : </div>
    Ligne 81 : <asp:Button ID="Button1" runat="server" Text="exporter en PDF" OnClick="Button1_Click" />
    Ligne 82 : </form>
    Ligne 83 : </body>

    Monday, November 5, 2018 10:39 AM

Answers

  • User839733648 posted

    Hi Omar27,

    The error occurs because your button click did not have an effect.

    I think that you may try the following two ways.

    • Change the .aspx code CodeBehind="xxx.aspx.cs" to Codefile="xxx.aspx.cs"
    • Try to delete the button "Button1_Click". Then clean and rebuild the code. Add a new button again and try.

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 6, 2018 3:48 AM

All replies

  • User1290358704 posted

    Is Button1_Click a member of your page class?

    Monday, November 5, 2018 9:00 PM
  • User839733648 posted

    Hi Omar27,

    The error occurs because your button click did not have an effect.

    I think that you may try the following two ways.

    • Change the .aspx code CodeBehind="xxx.aspx.cs" to Codefile="xxx.aspx.cs"
    • Try to delete the button "Button1_Click". Then clean and rebuild the code. Add a new button again and try.

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 6, 2018 3:48 AM
  • User-2094959909 posted

    Jenifer Jiang,

    Thank you it worked for me 

    i deleted the button and added a new one the copied the same code on it and it worked thank you.

    Tuesday, November 6, 2018 1:56 PM