Quantcast
Viewing all articles
Browse latest Browse all 10

Class File to Make Print Option on Web Page

Class file to make print option on web page

 

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.Web.SessionState;


/// <summary>
/// Summary description for PrintHelper
/// </summary>
public class PrintHelper
{
    public PrintHelper()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    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 Str = StringWrite.ToString();
       HttpContext.Current.Response.Clear();
       HttpContext.Current.Response.Write(Str);
       HttpContext.Current.Response.Write("<Script>Window.Print();</Script>");
       HttpContext.Current.Response.End();

    }
  
   
}


Viewing all articles
Browse latest Browse all 10

Trending Articles