ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.[Solved] Export Gridview to PDF exported with blank table in ASP.Net<p>Hi <a class="username" rel="Prasunjeet">Prasunjeet</a>,</p> <p>If you want to avoid updatepanel issue, so please use <span style="text-decoration: underline;"><span id="ctl00_ContentLeft_LabelBlogDetail">PostBackTrigger</span></span><span id="ctl00_ContentLeft_LabelBlogDetail"> </span>and gives his <span style="text-decoration: underline;">ControlId</span> .</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:ScriptManager ID=&#34;src&#34; runat=&#34;server&#34;&gt; &lt;/asp:ScriptManager&gt; &lt;asp:UpdatePanel ID=&#34;updPDF&#34; runat=&#34;server&#34;&gt; &lt;Triggers&gt; &lt;asp:PostBackTrigger ControlID=&#34;btnExport&#34; /&gt; &lt;/Triggers&gt; &lt;ContentTemplate&gt; &lt;asp:GridView ID=&#34;GridView1&#34; HeaderStyle-BackColor=&#34;#3AC0F2&#34; HeaderStyle-ForeColor=&#34;White&#34; RowStyle-BackColor=&#34;#A1DCF2&#34; AlternatingRowStyle-BackColor=&#34;White&#34; AlternatingRowStyle-ForeColor=&#34;#000&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; AllowPaging=&#34;true&#34; OnPageIndexChanging=&#34;OnPageIndexChanging&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;ContactName&#34; HeaderText=&#34;Contact Name&#34; ItemStyle-Width=&#34;150px&#34; /&gt; &lt;asp:BoundField DataField=&#34;City&#34; HeaderText=&#34;City&#34; ItemStyle-Width=&#34;100px&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; ItemStyle-Width=&#34;100px&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnExport&#34; runat=&#34;server&#34; Text=&#34;Export To PDF&#34; OnClick=&#34;ExportToPDF&#34; /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Data; using System.Data.SqlClient; using System.Configuration; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser Imports iTextSharp.text.pdf</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.BindGrid(); } } private void BindGrid() { string strConnString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers&#34;)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } } protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; this.BindGrid(); } protected void ExportToPDF(object sender, EventArgs e) { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { //To Export all pages GridView1.AllowPaging = false; this.BindGrid(); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); } } } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not IsPostBack Then Me.BindGrid() End If End Sub Private Sub BindGrid() Dim strConnString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As New SqlConnection(strConnString) Using cmd As New SqlCommand(&#34;SELECT * FROM Customers&#34;) Using sda As New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using dt As New DataTable() sda.Fill(dt) GridView1.DataSource = dt GridView1.DataBind() End Using End Using End Using End Using End Sub Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs) GridView1.PageIndex = e.NewPageIndex Me.BindGrid() End Sub Protected Sub ExportToPDF(sender As Object, e As EventArgs) Using sw As New StringWriter() Using hw As New HtmlTextWriter(sw) &#39;To Export all pages GridView1.AllowPaging = False Me.BindGrid() GridView1.RenderControl(hw) Dim sr As New StringReader(sw.ToString()) Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F) Dim htmlparser As New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Write(pdfDoc) Response.[End]() End Using End Using End Sub Public Overrides Sub VerifyRenderingInServerForm(control As Control) &#39; Verifies that the control is rendered End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/iTGBIyl.jpg" alt="" width="446" height="331" /></p>https://www.aspforums.net:443/Threads/189517/Solved-Export-Gridview-to-PDF-exported-with-blank-table-in-ASPNet/https://www.aspforums.net:443/Threads/189517/Solved-Export-Gridview-to-PDF-exported-with-blank-table-in-ASPNet/Fri, 20 Jul 2018 05:46:34 GMT