Search content within the blog

Monday, May 11, 2009

Printing in asp.net - part II

In this section we learn how we can pass controls of our choice for printing and also modify the contents before printing.

Add a helper class called PrintHelper.cs in your appcode first. This class is mainly used for printing the contents of our choice.

It is having a mrthod called webcontrol the control which we want to print.In the following example i group all the controls and contents i want to print into a htmltable and pass that table to this class.

The rest of the code is self explanatory....

PrintHelper.cs


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using 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 = new System.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.Write("<script>window.close();</script>");
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();

}
}

Default.aspx page code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintControls.aspx.cs" Inherits="Print_PrintControls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Panel ID="Panel1" runat="server" Height="161px" Width="714px">
First Name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br />
<br />
Last Name
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
Age           
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<br />
</asp:Panel>
</div>
<br />
<asp:Button ID="btnPrint" runat="server" OnClick="btnPrint_Click" Text="Print" />
</div>
<div id="divGatherInfo">
Enter the details and click print to print the contents dynamically......
<br />
Username :<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<br />
         Age :<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnPrintDynamic" runat="server" OnClick="btnPrintDynamic_Click" OnClientClick="return PrintDataDynamic('divDynamicAre')"
Text="Print" />
</div>
</form>
</body>
</html>

Default.aspx.cs page code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Print_PrintControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
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=300px,scrollbars=1');</script>");
}
protected void btnPrintDynamic_Click(object sender, EventArgs e)
{
HtmlTable tb = new HtmlTable();
tb.Width = "50%";
tb.Align = "center";
tb.Border = 1;
tb.CellPadding = 0;
tb.CellSpacing = 0;

HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tc = new HtmlTableCell();
tc.Width = "100%";
tc.InnerHtml = "<u><i>The following details were provided...</i><u>";
tr.Cells.Add(tc);
tb.Rows.Add(tr);

tr = new HtmlTableRow();
tc = new HtmlTableCell();
tc.Width = "50%";
tc.InnerText = "Username";
tc.Align = "center";
tr.Cells.Add(tc);

tc = new HtmlTableCell();
tc.Width = "50%";
tc.InnerText = "Password";
tc.Align = "center";
tr.Cells.Add(tc);
tb.Rows.Add(tr);

tr = new HtmlTableRow();
tc = new HtmlTableCell();
tc.Width = "50%";
tc.InnerText = txtUsername.Text.Trim();
tc.Align = "center";
tr.Cells.Add(tc);

tc = new HtmlTableCell();
tc.Width = "50%";
tc.InnerText = txtAge.Text.Trim();
tc.Align = "center";
tr.Cells.Add(tc);
tb.Rows.Add(tr);

Session["ctrl"] = tb;

ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
}
}


Print.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Print.aspx.cs" Inherits="Print" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Print Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

<u><i>Print.aspx.cs page code</i></u>

Print.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Print : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control ctrl = (Control)Session["ctrl"];
PrintHelper.PrintWebControl(ctrl);
}
}

No comments:

Post a Comment