Friday, December 16, 2016

How to Print in ASP.NET using JavaScript

ASP.NET Code: 

<asp:Panel ID="pnlContents" runat="server">
     Here will go your Code
</asp:Panel>
<asp:Button ID="btnPrint" runat="server" Text="Print" 
OnClientClick = "return PrintPanel();" />


JavaScript Code:

<script type = "text/javascript">
        function PrintPanel() {
            var panel = document.getElementById("<%=pnlContents.ClientID %>");
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>Print Page Header</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(panel.innerHTML);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            setTimeout(function () {
                printWindow.print();
            }, 500);
            return false;
        }
    </script>

pnlContents is the ID of ASP.NET Panel Element It. Which we Want to print. Just replace this Id name with yo