How To Print in ASP
How To Print in ASP
0
One of the most common functionality in any ASP.NET application is to print forms and controls. There are a lot of options to print forms using client scripts. In the article, we will see how to print controls in ASP.NET 2.0 using both server side code and javascript. Step 1: Create a PrintHelper class. This class contains a method called PrintWebControl that can print any control like a GridView, DataGrid, Panel, TextBox etc. The class makes a call to window.print() that simulates the print button. Note: I have not written this class and neither do I know the original author. I will be happy to add a reference in case someone knows. C# using using using using using using using using using using using using
System; System.Data; System.Configuration; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.IO; System.Text; System.Web.SessionState;
public class PrintHelper { public PrintHelper() { } public static void PrintWebControl(Control ctrl) { PrintWebControl(ctrl, string.Empty); } public static void PrintWebControl(Control ctrl, string Script) { StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = newSystem.Web.UI.HtmlTextWriter(stringWrite); if (ctrl is WebControl) { Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w; } Page pg = new Page(); pg.EnableEventValidation = false;
if (Script != string.Empty) { pg.ClientScript.RegisterStartupScript(pg.GetType(),"PrintJavaScript", Script); } HtmlForm frm = new HtmlForm(); pg.Controls.Add(frm); frm.Attributes.Add("runat", "server"); frm.Controls.Add(ctrl); pg.DesignerInitialize(); pg.RenderControl(htmlWrite); string strHTML = stringWrite.ToString(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Write(strHTML); HttpContext.Current.Response.Write("<script>window.print();</script>" ); HttpContext.Current.Response.End(); } }
VB.NET Imports Imports Imports Imports Imports Imports Imports Imports Imports Imports Imports Imports System System.Data System.Configuration System.Web System.Web.Security System.Web.UI System.Web.UI.WebControls System.Web.UI.WebControls.WebParts System.Web.UI.HtmlControls System.IO System.Text System.Web.SessionState
Public Class PrintHelper Public Sub New() End Sub Public Shared Sub PrintWebControl(ByVal ctrl As Control) PrintWebControl(ctrl, String.Empty) End Sub Public Shared Sub PrintWebControl(ByVal ctrl As Control, ByVal Script As String) Dim stringWrite As StringWriter = New StringWriter() Dim htmlWrite As System.Web.UI.HtmlTextWriter = NewSystem.Web.UI.HtmlTextWriter(stringWrite) If TypeOf ctrl Is WebControl Then Dim w As Unit = New Unit(100, UnitType.Percentage) CType(ctrl, WebControl).Width = w End If Dim pg As Page = New Page() pg.EnableEventValidation = False
If Script <> String.Empty Then pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script) End If Dim frm As HtmlForm = New HtmlForm() pg.Controls.Add(frm) frm.Attributes.Add("runat", "server") frm.Controls.Add(ctrl) pg.DesignerInitialize() pg.RenderControl(htmlWrite) Dim strHTML As String = stringWrite.ToString() HttpContext.Current.Response.Clear() HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.Write("<script>window.print();</script>") HttpContext.Current.Response.End() End Sub End Class
Step 2: Create two pages, Default.aspx and Print.aspx. Default.aspx will contain the controls to be printed. Print.aspx will act as a popup page to invoke the print functionality. Step 3: In your Default.aspx, drag and drop a few controls that you would like to print. To print a group of controls, place them all in a container control like a panel. This way if we print the panel using our PrintHelper class, all the controls inside the panel gets printed. Step 4: Add a print button to the Default.aspx and in the code behind, type the following code: C# protected void btnPrint_Click(object sender, EventArgs e) { Session["ctrl"] = Panel1; ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=30 0px,scrollbars=1');</script>"); } VB.NET Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventA rgs)Handles btnPrint.Click Session("ctrl") = Panel1 ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=30 0px,scrollbars=1');</script>") End Sub
The code stores the control in a Session variable to be accessed in the pop up page, Print.aspx. If you want to print directly on button click, call the Print functionality in the following manner : PrintHelper.PrintWebControl(Panel1);
Step 5: In the Page_Load event of Print.aspx.cs, add the following code: C# protected void Page_Load(object sender, EventArgs e) { Control ctrl = (Control)Session["ctrl"]; PrintHelper.PrintWebControl(ctrl); } VB.NET Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load Dim ctrl As Control = CType(Session("ctrl"), Control) PrintHelper.PrintWebControl(ctrl) End Sub
From Database
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Print Gridview Data in asp.net</title> <script type="text/javascript"> function PrintGridData() { var prtGrid = document.getElementById('<%=gvUserInfo.ClientID %>'); prtGrid.border = 0; var prtwin = window.open('', 'PrintGridViewData','left=100,top=100,width=1000,height=1000,toll
bar=0,scrollbars=1,status=0,resizable=1');
prtwin.document.write(prtGrid.outerHTML); prtwin.document.close(); prtwin.focus(); prtwin.print(); prtwin.close(); } </script> </head> <body> <form id="form1" runat="server"> <div> <b>Print Gridview Data</b><br /><br /> <asp:GridView ID="gvUserInfo" runat="server" > <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White"/> </asp:GridView> <input type="button" id="btnPrint" value="Print" onclick="PrintGridData()" /> </div> </form> </body> </html>
Code behind
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridview(); } } // This method is used to bind gridview from database protected void BindGridview() { using (SqlConnection con = new SqlConnection("Initial Catalog=MySampleDB;Data
Source=SureshDasari;Integrated Security=true"))
{ con.Open(); SqlCommand cmd = new SqlCommand("Select TOP 10 UserId,UserName,Location