C# .NET - print format

Asked By sundar on 26-Sep-11 09:58 AM
Dear sir,
           I need to print the webform details to print...
Sakshi a replied to sundar on 26-Sep-11 10:07 AM


 

You can do it in CodeBehind to a server-side Button:
   printButton.Attributes.Add("onclick", "JavaScript: window.print(); return false;")

Or
 
just add an HTML Button to your page:
   <INPUT type="button" value="Print" onclick="JavaScript:window.print();">

dipa ahuja replied to sundar on 26-Sep-11 10:12 AM
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')" />
 
Reena Jain replied to sundar on 26-Sep-11 10:22 AM
hi,

I would suggest you to use Report Viewer Control.You can do easily many things easily with report viewer control including printing and repeating headers on multiple pages without any effort.ReportViewer is a freely
redistributable control that enables embedding reports in applications developed using the .NET Framework. Reports are designed with drag-and-drop simplicity using Report Designer included in Visual Studio 2005 (Standard
editon and above.) There is a WinForms and a WebForms version of the control.
Find more info about report viewer control in below links:
http://www.gotreportviewer.com/

here is one more good link for you if you don't want to use report viewer
http://www.dotnetcurry.com/ShowArticle.aspx?ID=92

Hope this will help you
Suchit shah replied to sundar on 26-Sep-11 10:28 AM

JavaScript CODE:

<script language="JavaScript" type="text/JavaScript">
       
function doPrint() {
        bdhtml
=window.document.body.innerHTML;
        sprnstr
="<!--startprint-->";
        eprnstr
="<!--endprint-->";
        prnhtml
=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
        prnhtml
=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
        window
.document.body.innerHTML=prnhtml;
        window
.print();
       
}
</script>

HTML CODE:

<!--startprint-->
This area will print!
<!--endprint-->
<br />
I will not print?
<input id="btnPrint" type="button" value="Print" onclick="doPrint()" />



   How to print a part of a web page with JavaScript (2) [http://forums.asp.net/t/1360420.aspx#_top]

JavaScript CODE:

<script language="javascript" type="text/javascript">
   
function printdiv(divID)
   
{
     
var headstr = "<html><head><title></title></head><body>";
     
var footstr = "</body>";
     
var newstr = document.all.item(divID).innerHTML;
     
var oldstr = document.body.innerHTML;
      document
.body.innerHTML = headstr+newstr+footstr;
      window
.print();
      document
.body.innerHTML = oldstr;
     
return false;
   
}
</script>

HTML CODE:

<input name="b_print" type="button" onclick="printdiv('divID');" value=" Print " />
<div id="divID">
<h1 style="color:green">
The Div content which you want to print
</h1>

http://forums.asp.net/t/1360420.aspx
Anoop S replied to sundar on 26-Sep-11 03:13 PM
there is a simple way to do this: window.print().

All you need is a clientside print button.

Something like:

<button onclick="window.print();">Print!</button>

This will do much the same as a user clicking file print on their browser.