1
Posted on 6:20 AM by prajeesh and filed under , ,
Printing is a common feature needed in almost every type of web pages, there is a plenty of options to include print feature in your web page, i would like to describe two methods.

1.Printing Using java script
It is a simple task

This HTML code will return a Print button and you can click this button to print your web page.

2.Printing using web controls using ASP.net

Little programming effort is needed for printing web controls in your ASP.net application.
If you want to print contents of a web control in your ASP.net page, you can use following code segment, it is also using javascript for printing but it prints selected control only.
///
/// Prints any web control Grid view, DataGrid, Page , Panel etc.
///
///

public static void PrintWebControl(Control ctrl)
{
String Script;
StringWriter stringWrite = new StringWriter ();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
if ( ctrl is WebControl )
{
Unit w = new Unit(400, UnitType.Pixel);
((WebControl)ctrl).Width = w;
}
Page pg = new Page ();
if (Script != string.Empty )
{
pg.RegisterStartupScript ("PrintJavaScript",Script);
}
HtmlForm frm = new HtmlForm ();
pg.Controls.Add (frm);
frm.Attributes.Add ("runat","server");
frm.Controls.Add (ctrl);
string scr = " ";
htmlWrite.Write(scr);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write(" ");
HttpContext.Current.Response.End();
}
You can pass the control to be printed as a parameter of PrintWebControl(Control ctrl) function.
Happy coding Shout it kick it on DotNetKicks.com
1
Response to ... Printing a web page using ASP.net
Saiful Alam said... November 4, 2008 at 7:46 AM

Nice blog...