当我尝试打印时,CSS无法正常工作 [英] The CSS not working when I try to take print

查看:65
本文介绍了当我尝试打印时,CSS无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class _Default:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{



}

protected void Button1_Click(对象发送者,EventArgs e)

{

Session [ctrl] = Panel1;

控制ctrl =(控制)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是WebControl)

{

单位w =新单位(100,UnitType.Percentage);

((WebControl)ControlToPrint).Width = w;

}

Page pg = new Page();

pg.EnableEventVa lidation = 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();

}

}

我尝试过使用Java脚本,但问题仍然存在一样的我不明白为什么CSS没有工作。

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 .

推荐答案

如果你想用CSS 屏幕(浏览器),打印所有渲染目的地你应该使用指定media 属性。

i)在下一个例子中,CSS文件将用于所有(屏幕和打印):

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):
<link href="@Url.Content("~/Content/CustomerAdmin.css")" rel="stylesheet" type="text/css" media="all" />



ii)在下一个示例中,将使用CSS文件仅用于打印:


ii)In the next example the CSS file will be used only for print:

<link href="@Url.Content("~/Content/CustomerAdmin.css")" rel="stylesheet" type="text/css" media="print" />



您可以在下一个链接中找到有关此内容的更多详细信息:< a href =http://www.w3.org/TR/CSS21/media.html> http://www.w3.org/TR/CSS21/media.html [ ^ ]






有些方法可以使用@media进行打印,但很多时候这种方式也行不通。

所以相反,它会很好如果你可以创建你想要用设计打印的数据的图像然后打印它。



谢谢
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


这篇关于当我尝试打印时,CSS无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆