【问题标题】:printing problem in asp.net webapplicationasp.net web应用程序中的打印问题
【发布时间】:2010-12-09 17:58:46
【问题描述】:

我正在使用以下代码打印 Web 应用程序的内容 -

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;

public class PrintHelper
{
    public PrintHelper()
    {
    }

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

这里我将文本框控件传递给 PrintWebControl() 方法。

在打印按钮上点击我写 -

protected void btnPrint_Click(object sender, EventArgs e)
    {
        Session["ctrl"] = Panel1;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
    }

现在的问题是,当我运行此应用程序并按下打印按钮时,打印机只打印了多行文本框的一半内容,而其余部分则不打印。

所以告诉我如何使用上面的代码打印多行文本框。请注意,我的多行文本框有大量数据,可以打印超过 4 或 5 页。

如果你有其他代码,请与我分享

提前谢谢大家

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我想你在这里错过了一些概念。

    HttpContext.Current.Response.Clear();
    

    将清除您的响应标头。然后你调用 'HttpContext.Current.Response.Write(...)' 这将被读取为纯文本内容而不是网页内容。

    如果您只想打印网页的某些特定部分,请使用 css

    <style media="print">
    ...
    </style>
    

    并像普通的asp页面一样设置页面的内容。 iframe 也可能有助于通过将数据设置为 iframe 将其他所有内容设置为 display:none(in css media="print") 并将 iframe 设置为 display:block。

    编辑

    在当前页面中

    <script type="text/javascript">
     function openPopup(){
      window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');
     }
    </script>
    

    在当前页面的打印按钮中(使用 input type="button")

    <input type="button" value="Print" onclick="openPopup()"/>
    

    调用openPopup 页面。然后在 print.aspx 中创建您需要的普通 aspx 页面和初始值。和

    **print.aspx
    <script>
     function print(){
      window.print();
     }
    </script>
    ...
    <body onload="print();">
    

    过程是这样的 1 位用户单击当前页面中的打印按钮 2 openPopup(); 3 print.aspx 正在加载 4 初始化你需要的所有值 5 print.aspx 页面中的 'print()' 将在 print.aspx 准备就绪时触发。

    【讨论】:

    • 实际上我正在了解如何使用 css 进行打印,因为我是 c#.net 的新手,所以请简要告诉我或发送代码我是如何做到的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2013-05-31
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多