Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Session["ctrl"] = Panel1;
Control ctrl = (Control)Session["ctrl"];
PrintWebControl(ctrl);
}
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("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
}
I have tried using Java scripts but the problem remains the same . I am not understanding why CSS is not Working .
Posted
Comments
Sergey Alexandrovich Kryukov 12-Dec-14 1:32am    
Well, background may not work, but alignment, fonts, etc. should work. Also, you may use @media rules...
—SA
Member 11111143 12-Dec-14 2:13am    
Actually the alignment is not working. This is the major problem.one page is taking into 2 pages

If you want to use a CSS for screen (browser), printor for all "rendering destination" you should specify this by using the media attribute.
i)In the next example the CSS file will be used for all (screen and print):
HTML
<link href="@Url.Content("~/Content/CustomerAdmin.css")" rel="stylesheet" type="text/css" media="all" />

ii)In the next example the CSS file will be used only for print:
HTML
<link href="@Url.Content("~/Content/CustomerAdmin.css")" rel="stylesheet" type="text/css" media="print" />

You can find more details about this in the next link: http://www.w3.org/TR/CSS21/media.html[^]
 
Share this answer
 
v2
Hi,

There are some ways like you can use @media for printing but many time this also not works.
So instead of that it will good if you can create image of your data which you want to print with design and then print it.

Thanks
 
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