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 strHTML = stringWrite.ToString(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Write(strHTML); HttpContext.Current.Response.Write("<script>window.print();</script>"); HttpContext.Current.Response.End(); } } //on button click Session["ctrl"] = FormView1; ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=auto,width=300px,scrollbars=1');</script>"); PrintHelper.PrintWebControl(FormView1); //default page load Control ctrl = (Control)Session["ctrl"]; PrintHelper.PrintWebControl(ctrl);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)