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


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Printing Pages

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

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

Hi,

         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

   3)Tables 

    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();

            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("");

            HttpContext.Current.Response.End();

        }

    }

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

 

 {

        Session["businessprint"] = pnl_print;

        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "");

    }


WORK FlOW:

 

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

PrintHelper.PrintWebControl(ssd);

 

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!!!!

 

Cheers!!!!!!

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

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