How to export ascx page gridview display to PDF?Usning below code GrdView1 loaded data sucessfully.Now When click export button same gridview data need to export to PDF.I tried many example available in different forums but nothing works out in ascx
page.
protected void Button1_Click(object sender, EventArgs e)
{
GridView gv = (GridView)WebUserControl1.FindControl("GridView1"); Response.AddHeader("content-disposition", "attachment;filename=Vithal_Wadje.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); gv.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(); gv.AllowPaging = true; gv.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
132 Points
504 Posts
ascx grid view export to PDF
Sep 16, 2020 04:33 PM|jula|LINK
How to export ascx page gridview display to PDF?Usning below code GrdView1 loaded data sucessfully.Now When click export button same gridview data need to export to PDF.I tried many example available in different forums but nothing works out in ascx page.
SqlDataSource1.SelectCommand = "Query"
SqlDataSource1.Select(DataSourceSelectArguments.Empty)
SqlDataSource1.DataBind()
GrdView1.DataBind()
All-Star
53081 Points
23655 Posts
Re: ascx grid view export to PDF
Sep 16, 2020 04:43 PM|mgebhard|LINK
Generally, a PDF API is used to generate a PDF. What PDF API are you using?
Contributor
3730 Points
1431 Posts
Re: ascx grid view export to PDF
Sep 17, 2020 05:35 AM|yij sun|LINK
Hi jula,
Could you tell us where do you add the codes of export to PDF?Do you have errors?
I suggest you could use iTextSharp to fix it.Since you don't post your codes,I have created a demo for you.
Usercontrol.ascx:
Usercontrol.ascx Code-Behind:
Aspx page:
Aspx Code-Behind:
protected void Button1_Click(object sender, EventArgs e) { GridView gv = (GridView)WebUserControl1.FindControl("GridView1");
Response.AddHeader("content-disposition", "attachment;filename=Vithal_Wadje.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gv.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();
gv.AllowPaging = true;
gv.DataBind(); } public override void VerifyRenderingInServerForm(Control control) { /* Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. */ }
More details,you could refer to below article:
https://www.c-sharpcorner.com/UploadFile/0c1bb2/export-gridview-to-pdf/
https://forums.asp.net/t/2055132.aspx?ascx+page+have+a+grid+that+grid+data+to+be+export+to+pdf+excel+and+print+inside+the+aspx+page+that+page+inside+of+master+page+
Best regards,
Yijng Sun