ASP.NET - print using asp.net

Asked By Bala Marish on 19-Aug-11 03:52 AM


i created one asp.net application


on my page i use one button as print...

when i click print button the new window was opened using window.open method....

i want open a new window as well as print the newly opened window how to do this....    
dipa ahuja replied to Bala Marish on 19-Aug-11 04:09 AM
For popup page:

<asp:Button ID="Button5" runat="Server" Text="New Entry"
OnClientClick="window.open('~/email.asxp');" />

For print:


Untitled document
<script type="text/javascript">
   function CallPrint(strid) {
     var prtContent = document.getElementById(strid);
     var WinPrint = window.open('', '', 'letf=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
     WinPrint.document.write(prtContent.innerHTML);
     WinPrint.document.close();
     WinPrint.focus();
     WinPrint.print();
     WinPrint.close();
 
   }
</script>
 
 
<div id="divPrint">
   <%--Put Controls that you want to print--%>
</div>
 
<input type="button" value="print " id="btnPrint" runat="Server"
onclick="javascript:CallPrint('divPrint')" />
 
Anoop S replied to Bala Marish on 19-Aug-11 04:13 AM
its really easy to print a page using a simple link on a page. Write the following:

<a href="javascript:window.print()">Print Page</a>

When you click this link in your browser a print page will open. You might also be interested in using a separate .CSS file for printing pages. You can find a simple sample by reading a following article: http://www.alistapart.com/articles/goingtoprint/
Jitendra Faye replied to Bala Marish on 19-Aug-11 04:22 AM

Use this code to Print the page.

<html>
    <head>
      <title>Print</title>
      <link rel="stylesheet" type="text/css" media="all" href="all.css" />
      <link rel="stylesheet" type="text/css" media="print" href="print.css" />
    </head>
<body>
    <p>I get printed</p>
    <form>
      <input type="button" onclick="window.print()" value="Print" />
    </form>
</body>
</html>

Hope this will help you.

 

Web Star replied to Bala Marish on 19-Aug-11 04:24 AM
if you want to open print page in new window and same time it will print then you just put window.print on page load of new window print page.

eg suppose print button on abc.aspx page

you put code for open new window on button click as follows

btnPrint.Attribute.Add("OnClick", "javascript:printWinodw();");

In javascript funciton on abc.aspx page as follows

function printWinodw()
{
window.open('printpage.aspx','name','height=200,width=150');
}

And finally you need window.print on body tag of printpage.aspx for print automatically when page open in popup window.
<script language="Javascript1.2">
  <!--
  function printpage() {
  window.print();
  }
  //-->
</script>
<body onload="printpage()">
TSN ... replied to Bala Marish on 19-Aug-11 04:36 AM
hi..here are steps follow this..

Step 1: Create a PrintHelper class. This class contains a method called PrintWebControl that can print any control like a GridView, DataGrid, Panel, TextBox etc. The class makes a call to window.print() that simulates the print button.
Note: I have not written this class and neither do I know the original author. I will be happy to add a reference in case someone knows. 
C# 
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();
    }
}
Step 2: Create two pages, Default.aspx and Print.aspx. Default.aspx will contain the controls to be printed. Print.aspx will act as a popup page to invoke the print functionality.
Step 3: In your Default.aspx, drag and drop a few controls that you would like to print. To print a group of controls, place them all in a container control like a panel. This way if we print the panel using our PrintHelper class, all the controls inside the panel gets printed.
Step 4: Add a print button to the Default.aspx and in the code behind, type the following code:
C#
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>");
    }
Step 5: In the Page_Load event of Print.aspx.cs, add the following code:
C#
 
protected void Page_Load(object sender, EventArgs e)
    {
      Control ctrl = (Control)Session["ctrl"];
      PrintHelper.PrintWebControl(ctrl);
    }