This
article will show you how you can do Ajax export gridview to PDF without
refreshing the page in asp.net using c#.net and jQuery.
Some of my
previous articles are as follows: Enable
and Disable ADO.NET and OLEDB Connection Pooling In .Net, Reading
XML Document in C# Using Linq With Where Clause In Asp.Net, Reading
XML Document in C# Using Linq and Bind To GridView In Asp.Net, Export
GridView Or Table Data Into jSon Format Data By C#.Net In Asp.Net Using jQuery, Retrieve
Connection String From Web.config In ASP.net Using C#.Net
(AppSettings,ConnectionStrings), Html
Table With Its Attributes Or Css Table Attributes, Bind
GridView To DataTable and Convert GridView Data Into Chart In Asp.Bet Using
jQuery, Access
Hidden Or HiddenFor Fields Value At Controller End In Asp.Net Mvc Using C#.
So for this
article first we will create a new asp.net application and add a page. Now add
the below mention JavaScript library reference into header of the page.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/sprintf.js"></script>
<script type="text/javascript" src="js/jspdf.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="js/tableExport.js"></script>
|
Now add a grid view control and a button on page. After adding all the content your page will look as shown below:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Export GridView Or
Table Data Into PDF By C#.Net In Asp.Net Using jQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/sprintf.js"></script>
<script type="text/javascript" src="js/jspdf.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="js/tableExport.js"></script>
<script>
function ExportPdf() {
$('#<%=GridView1.ClientID %>').tableExport({ type: 'pdf', escape: 'false',htmlContent:'false' });
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server"></asp:GridView>
<br />
<input type="button" value="Export to
PDF" id="btnExport"
onclick="javascript: ExportPdf();" target="blank" />
</div>
</form>
</body>
</html>
|
Now open the .cs page and add the below code.
using System;
using
System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (!IsPostBack)
{
dt
= GetDataToBind();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
public DataTable GetDataToBind()
{
DataTable dt = new DataTable("SaleData");
dt.Columns.Add(new DataColumn("Month", typeof(string)));
dt.Columns.Add(new DataColumn("Sale", typeof(string)));
DataRow dr1 =
dt.NewRow();
dr1["Month"] = "Group 1";
dr1["Sale"] = "10000";
dt.Rows.Add(dr1);
DataRow dr2 =
dt.NewRow();
dr2["Month"] = "Group 2";
dr2["Sale"] = "34300";
dt.Rows.Add(dr2);
DataRow dr3 =
dt.NewRow();
dr3["Month"] = "Group 3";
dr3["Sale"] = "23400";
dt.Rows.Add(dr3);
DataRow dr4 =
dt.NewRow();
dr4["Month"] = "Group 4";
dr4["Sale"] = "30040";
dt.Rows.Add(dr4);
DataRow dr5 =
dt.NewRow();
dr5["Month"] = "Group 5";
dr5["Sale"] = "3422";
dt.Rows.Add(dr5);
return dt;
}
}
}
|
In above code I have bind the data of datatable to the gridview. Now we have done run the application to check the output.
Here is the gridview.
Now click on button to export the pdf.
0 comments:
Please let me know your view