本文介绍了Css Style在打印时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个pannel里面我有一个gridview
i二手静态类打印助手
i用于发送面板作为参数printpreview显示但css样式我应用没有得到
------------------ -PrintHelperClass -------------------------------
i have a pannel inside it i had an gridview
i used static class Print helper
i used to send panel as arguments printpreview is showing but css style i applied not getting
-------------------PrintHelperClass-------------------------------
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.Text;
using System.Web.SessionState;
//using System.Web.HttpContext;
/// <summary>
/// Summary description for PrintHelper
/// </summary>
public class PrintHelper
{
public PrintHelper()
{
}
public static void PrintWebControl(Control ctrl)
{
PrintWebControl(ctrl, string.Empty);
}
public static void PrintWebControl(Control ctrl, string Script)
{
//if (System.Web.HttpContext.Current.(string)Session["isgrid"] == "grid")
//{
// GridView grid = (GridView)ctrl;
// grid.AllowPaging = false;
// grid.DataBind();
//}
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();
}
}.
-----------------clickevent for print-----------------
protected void btnPrint_Click(object sender, ImageClickEventArgs e)
{
Session["ctrl"] = dgvRegisterReport;
ScriptManager.RegisterStartupScript(this, this.GetType(), "onclick", "javascript:window.open( 'http://localhost:57601/PolosysWebERP5.2_1/GeneralPages/Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');", true);
}
-------------------------------
for grid i given style but while printing the style is not getting
i used @media print also then also not getting.
推荐答案
protected void btnPrint_Click(object sender, ImageClickEventArgs e)
{
Session["GridView"] = dgvRegisterReport;
Session["ctrl"] = pnlContents;
ScriptManager.RegisterStartupScript(this, this.GetType(), "onclick", "javascript:window.open( 'http://localhost:57601/PolosysWebERP5.2_1/GeneralPages/Print.aspx','PrintMe','height=600px,width=600px,scrollbars=1');", true);
}
on print.aspx page
protected void Page_Load(object sender, EventArgs e)
{
Control ctrl = (Control)Session["ctrl"];
GridView GridView1 = (GridView)Session["GridView"];
PrintHelper.PrintWebControl(ctrl,string.Empty,GridView1);
}
class file :
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.Text;
using System.Web.SessionState;
//using System.Web.HttpContext;
/// <summary>
/// Summary description for PrintHelper
/// </summary>
public class PrintHelper
{
public PrintHelper()
{
}
//public static void PrintWebControl(Control ctrl)
//{
// PrintWebControl(ctrl, string.Empty,GridView salesgridview);
//}
public static void PrintWebControl(Control ctrl, string Script, GridView SalesGridView)
{
//if (System.Web.HttpContext.Current.(string)Session["isgrid"] == "grid")
//{
// GridView grid = (GridView)ctrl;
// grid.AllowPaging = false;
// grid.DataBind();
//}
SalesGridView.UseAccessibleHeader = true;
SalesGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
SalesGridView.FooterRow.TableSection = TableRowSection.TableFooter;
SalesGridView.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in SalesGridView.Rows)
{
if (row.RowIndex % 30 == 0 && row.RowIndex != 0)
{
row.Attributes["style"] = "page-break-after:always;";
}
}
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>");
string style = "<style type="text/css">td { padding: 4px; border: solid 1px #c1c1c1; color: #474545; font-family:Arial; font-size:12px } th { padding: 2px 2px; color: #3e4d9e; background: #e0d7f4 ; font-family:Arial; text-align:left;text-indent:10px; font-size:14px ;border-top: solid 1px #a99cec; border-left:none;border-right:none; border-bottom:none }{ width: 100%;background-color: #fff; margin: 5px 0 10px 0; border-top: solid 1px #a99cec; border-left:solid 1px #a99cec;border-right:solid 1px #a99cec;font-family:Arial; font-size:15px; </style>";
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.End();
}
}
这篇关于Css Style在打印时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!