Create a web form like below. Put all the controls inside one Panel.
On click event of the “Print Page” button write below code.
//Write the below code for click event of the "Print Page" button
protected void btnPrintPage_Click(object sender,EventArgs e)
{
Session["ctrl"] = Panel1;
Control ctrl = (Control)Session["ctrl"];
PrintWebControl(ctrl);
}
using System.IO;
Create the function PrintWebControl which is calling from the click event of the “Page Print” button.
public static void PrintWebControl(Control ControlToPrint)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ControlToPrint is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ControlToPrint).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ControlToPrint);
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();
}
On click event of the “Print Page” button write below code.
//Write the below code for click event of the "Print Page" button
protected void btnPrintPage_Click(object sender,EventArgs e)
{
Session["ctrl"] = Panel1;
Control ctrl = (Control)Session["ctrl"];
PrintWebControl(ctrl);
}
using System.IO;
Create the function PrintWebControl which is calling from the click event of the “Page Print” button.
public static void PrintWebControl(Control ControlToPrint)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ControlToPrint is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ControlToPrint).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ControlToPrint);
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();
}
No comments:
Post a Comment