使用ASP.NET打印Web控件时隐藏页眉和页脚

时间:2022-10-25 07:25:03

How can I hide the header like 'Page 1 of 1' and footer (url) when printing a webcontol in ASP.NET?

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

I currently doing by opening a new page on Print button click ande in it

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

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

This will print the header and footer. How to avoid it?

这将打印页眉和页脚。怎么避免呢?

4 个解决方案

#1


That setting is configured by the user in their browser. Their is no way to disable it from code. You'r best bet is to include instructions on how to configure/disable the settings.

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

See an example here: http://www.xheo.com/products/sps/default.aspx?print=true

请参阅此处的示例:http://www.xheo.com/products/sps/default.aspx?print = true

#2


You should use CSS styles and specify they apply to a media type of print. See this article for help; http://www.cantoni.org/articles/printstyle

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

Basically create a seperate stylesheet for print styles only. If you want to hide something on the page use { display:none } as one of that elements style attributes. Then link your stylesheet in the HEAD element;

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

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

#3


For this we are using print class like it

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

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();
}

here just change a line in form property and set

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

frm.ResolveUrl("");

so URl is not visible when page is print..i use it successfuly.

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

#4


If you're using Firefox, and you can have the client install this add-on.

如果您使用的是Firefox,则可以让客户端安装此附加组件。

Otherwise, if you are using Internet Explorer: google for "MeadCo ScriptX"

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

(FF option is free, IE option is free for basic functionality only)

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

#1


That setting is configured by the user in their browser. Their is no way to disable it from code. You'r best bet is to include instructions on how to configure/disable the settings.

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

See an example here: http://www.xheo.com/products/sps/default.aspx?print=true

请参阅此处的示例:http://www.xheo.com/products/sps/default.aspx?print = true

#2


You should use CSS styles and specify they apply to a media type of print. See this article for help; http://www.cantoni.org/articles/printstyle

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

Basically create a seperate stylesheet for print styles only. If you want to hide something on the page use { display:none } as one of that elements style attributes. Then link your stylesheet in the HEAD element;

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

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

#3


For this we are using print class like it

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

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();
}

here just change a line in form property and set

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

frm.ResolveUrl("");

so URl is not visible when page is print..i use it successfuly.

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

#4


If you're using Firefox, and you can have the client install this add-on.

如果您使用的是Firefox,则可以让客户端安装此附加组件。

Otherwise, if you are using Internet Explorer: google for "MeadCo ScriptX"

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

(FF option is free, IE option is free for basic functionality only)

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