Hi Mehram,
remove the dlFabric.DataBind() line from the lnkPdf_Click event handler.
Please refer below sample.
HTML
<asp:DataList ID="dlFabric" runat="server" RepeatDirection="Horizontal" RepeatColumns="1">
<HeaderTemplate>
<table>
<tr>
<th>S. #</th>
<th>Artical #</th>
<th>Stock Qty</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblSNo" runat="server" Text='<%#Eval("row_num") %>'></asp:Label></td>
<td><asp:Label ID="lblArticalNo" runat="server" Text='<%#Eval("articalno") %>'></asp:Label></td>
<td><asp:Label ID="lblBalanceQty" runat="server" Text='<%#Eval("BalanceQty") %>'></asp:Label></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
<asp:Button ID="lnkPdf" runat="server" Text="Export to Pdf" OnClick="btnExport_Click" />
Namespaces
C#
using System.IO;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
VB.Net
Imports System.IO
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("row_num"), new DataColumn("articalno"), new DataColumn("BalanceQty") });
dt.Rows.Add(1, "1441", "1234");
dt.Rows.Add(2, "1567", "1896");
dt.Rows.Add(3, "1571", "1347");
dt.Rows.Add(4, "1231", "1977");
dt.Rows.Add(5, "1571", "1347");
dt.Rows.Add(6, "1571", "1347");
dlFabric.DataSource = dt;
dlFabric.DataBind();
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
dlFabric.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("row_num"), New DataColumn("articalno"), New DataColumn("BalanceQty")})
dt.Rows.Add(1, "1441", "1234")
dt.Rows.Add(2, "1567", "1896")
dt.Rows.Add(3, "1571", "1347")
dt.Rows.Add(4, "1231", "1977")
dt.Rows.Add(5, "1571", "1347")
dt.Rows.Add(6, "1571", "1347")
dlFabric.DataSource = dt
dlFabric.DataBind()
End If
End Sub
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
dlFabric.RenderControl(hw)
Dim sr As StringReader = New StringReader(sw.ToString())
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Sub
Screenshot