asp.net webapplication中的打印问题

时间:2010-12-08 12:38:38

标签: c# asp.net

我使用以下代码打印Webapplication的内容 -

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页以上打印。

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

提前感谢你们所有人

1 个答案:

答案 0 :(得分:0)

我想你在这里想念一些概念。

HttpContext.Current.Response.Clear();

将清除您的回复标题。然后你调用' HttpContext.Current.Response.Write(...)',它将被视为纯文本内容而不是网页内容。

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

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

并设置页面的内容,就像普通的asp页面一样。 iframe也可以通过将您的数据设置为iframe来帮助显示所有其他内容:none(在css media =“print”中)并设置iframe以显示:block。

修改

在当前页面

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

并在当前页面的打印按钮中(使用输入类型=“按钮”)

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

调用openPopup页面。然后在print.aspx中创建正常的aspx页面和你需要的init值。和

**print.aspx
<script>
 function print(){
  window.print();
 }
</script>
...
<body onload="print();">
这个过程是这样的 1用户单击当前页面中的“打印”按钮 2 openPopup(); 3 print.aspx正在加载 4初始化你需要的所有价值 5在print.aspx页面'print()'将在print.aspx准备就绪时被触发。

相关问题