开发者

printing problem in asp.net webapplication

i am using following code for print the content of 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();
    }
}

here i pass the textbox control to PrintWebControl() method.

on print button click i write -开发者_开发问答

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

now problem is that when i run this application and press on print button then only half of matter of multiline textbox is printed by the printer but rest part not.

so tell me how i print the multiline textbox using above code. Note my multiline textbox has huge data which will print on more than 4 or 5 page.

if you have another code for do it please share with me

thank to all of you in advance


i think u miss some concept here.

HttpContext.Current.Response.Clear();

will clear your response header. and then u call 'HttpContext.Current.Response.Write(...)' which will be read as pure text content not a web page content.

if u want to print only some specific section of web page, use css

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

and set page's content like normal asp page. also iframe might help by setting your data to iframe set everything else to display:none(in css media="print") and set iframe to display:block.

edit

in current page

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

and in print button in current page(use input type="button")

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

to call openPopup page. And then in print.aspx create normal aspx page and init values u need. and

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

the process goes like this 1 user click Print button in current page 2 openPopup(); 3 print.aspx being load 4 init all values u need 5 in print.aspx page 'print()' will be triggered when print.aspx is ready.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜