Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C# code for print with pagesettings in web applications
C#
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);
Posted
Updated 19-Mar-12 19:32pm
v3
Comments
Dean Oliver 19-Mar-12 2:14am    
Please provide your attempted code.
Member 8587874 19-Mar-12 2:56am    
Class i used
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);
Member 8587874 20-Mar-12 4:44am    
sorry sir its not helpful to me,by putting doublequotes for javascript it shows error such as semicolon,closing bracket expected like this.sir is it possibel to use current browser print control in c# web application,if so please provide me the code,i need the print properties seen in internet explorer or any other browser,ie perfect print with page settings,printpreview like.If not how can we set the header for a printpage.
Sergey Alexandrovich Kryukov 19-Mar-12 2:16am    
...and ask your questions. :-)
--SA
Member 8587874 19-Mar-12 2:37am    
Class i used
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);

1 solution

Do not post the same question twice.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900