.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Resource Bookmark and Share   

Printing of webpages

Posted By:Gowthammanju       Posted Date: June 14, 2011    Points: 200    Category:    URL: http://www.dotnetspark.com  

This article explains you about the printing of the particular webpages by clicking the print button.


         This article explains the user about how to print the pages through webpages in asp.net.

          After referring the various sources and links there by finally found the solution for printing the pages through asp.net.

          By this article we can print following items such as

   1) Grid view 

   2) Current Page of website


    4)images  etc......!

just follow the below steps you will find the solution!

step 1) Open the new website in visual studio.

step 2) On solution explorer right click the project and choose add new item tab

step 3) Select the class file and named as print.cs,type the coding as below

use header file as  using System.IO;//printer

coding as

public class print
	public print()
		// TODO: Add constructor logic here
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;
            System.Web.UI.Page pg = new System.Web.UI.Page();
            pg.EnableEventValidation = false;
            if (Script != string.Empty)
                pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
            HtmlForm frm = new HtmlForm();
            frm.Attributes.Add("runat", "server");
            string strHTML = stringWrite.ToString();

step 4) Now add two pages named as default.aspx and print.aspx page in to your project

step 5) Now do designing for the default.aspx pages as u like

step 6) Place the button named as print and double click on it.

step 7) Now type the following coding as below

protected void Button1_Click(object sender, EventArgs e)
        Session["businessprint"] = pnl_print;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "");

step 8) In the print.aspx page type the following coding as

//write in page load event 

            Control ssd = (Control)Session["businessprint"];
        catch (Exception) { }

In this the total page is being assigned in a session values through panel and finally passed to print page through javascript.

These are those values through which the session stores the total page details in default.aspx page

Session["businessprint"] = pnl_print

where pnl_print is the panel name 

and these values in session are retrieved in the print.aspx page through

Control ssd = (Control)Session["businessprint"];

where ssd is the dummy variable used to retrieve the passed session values

and finally it is passed to the class file through


where PrintHelper is the class name which we are created and ssd is the session values passed from print.aspx for printing purpose.....

for more details kindly download the attachment and verify it!

Print page:

Hence you got the solution!!!!







Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend