【问题标题】:Hide header and footer while printing a webcontrol using ASP.NET使用 ASP.NET 打印 Web 控件时隐藏页眉和页脚
【发布时间】:2009-06-17 07:49:28
【问题描述】:

在 ASP.NET 中打印 webcontol 时,如何隐藏诸如“第 1 页,共 1 页”和页脚 (url) 之类的页眉?

我目前通过在打印按钮上打开一个新页面单击并在其中进行操作

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

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

【问题讨论】:

    标签: asp.net printing web-controls printing-web-page


    【解决方案1】:

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

    在此处查看示例: http://www.xheo.com/products/sps/default.aspx?print=true

    【讨论】:

      【解决方案2】:

      您应该使用 CSS 样式并指定它们适用于印刷媒体类型。请参阅本文寻求帮助; http://www.cantoni.org/articles/printstyle

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

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

      【讨论】:

      • 这在打印时肯定会有所帮助,但仍然不能消除用户在浏览器中选择的设置……至少对于 IE 没有。
      • 对,我稍微误解了这个问题,我看到它们的意思是实际的浏览器打印页眉和页脚。我猜 Firefox 可能在某个地方有这种能力,但 IE 太封闭了。
      【解决方案3】:

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

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

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

      frm.ResolveUrl("");
      

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

      【讨论】:

        【解决方案4】:

        如果您使用的是 Firefox,则可以让客户端安装 this add-on

        否则,如果您使用 Internet Explorer:谷歌搜索“MeadCo ScriptX

        (FF 选项免费,IE 选项仅对基本功能免费)

        【讨论】:

          猜你喜欢
          • 2010-10-01
          • 2015-09-12
          • 1970-01-01
          • 2016-01-11
          • 1970-01-01
          • 2021-03-14
          • 2018-10-22
          • 1970-01-01
          • 2011-11-01
          相关资源
          最近更新 更多