在ASP.NET中打印webcontol时,如何隐藏“Page 1 of 1”和页脚(url)等标题?

我目前在打印按钮上打开一个新页面,单击其中的ande

protected void Page_Load(object sender, EventArgs e)
{
    if( null != Session["Control"] )
    {
        Control ctrl = ( Control )Session["Control"];
        PrintManager.PrintWebControl( ctrl );
        Session["Control"] = null;
    }
}

这将打印页眉和页脚。如何避免呢?

有帮助吗?

解决方案

该设置由用户在其浏览器中配置。他们无法从代码中禁用它。您最好的选择是包含有关如何配置/禁用设置的说明。

在此处查看示例: http://www.xheo.com/products/sps/default。 ASPX?打印=真

其他提示

您应该使用CSS样式并指定它们适用于媒体类型的打印。请参阅此文章以获取帮助; http://www.cantoni.org/articles/printstyle

基本上只为打印样式创建单独的样式表。如果要在页面上隐藏某些内容,请使用 {display:none} 作为其中一个元素样式属性。然后在HEAD元素中链接样式表;

<link href="print.css" media="print" type="text/css" rel="stylesheet" />

为此,我们使用类似的印刷类

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);
    // done changes on below 1 line for unassigned header URL //
    frm.ResolveUrl("");
    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();
}

这里只需更改form属性中的一行并设置

frm.ResolveUrl("");

所以当页面打印时,URl不可见..我成功地使用它。

如果你正在使用Firefox,你可以安装客户端这个附加

否则,如果您使用的是Internet Explorer:google for“ MeadCo ScriptX

(FF选项是免费的,IE选项仅限基本功能免费使用)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top