Showing posts with label asp net print div contents only. Show all posts
Showing posts with label asp net print div contents only. Show all posts

Tuesday, May 17, 2016

Print DIV content using JavaScript in asp.net

In this article I am going to explain how to print div content in ASP. net c#

Code of aspx page to print div

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintDiv.aspx.cs" Inherits="PrintDiv" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="PrintDiv">
    <table>
    <tr>
    <td style="padding:10px; color:#FF99CC; font-size:14px; font-weight:bold;">Hello ! Welcome at www.dotnetDarpan.blogspot.com</td>
    </tr>
     <tr>
    <td>Note: For any query mail us on  <b>gupta.sharad2010@gmail.com</b></td>
    </tr>
    </table>
    </div>
 
    <asp:Button ID="btnPrint" Text="Print" runat="server"  OnClientClick="PrintDiv();"/>
    </form>
 
    <script type="text/javascript">
        function PrintDiv() {
            var PrintDiv = document.getElementById("PrintDiv").innerHTML;
            var printWindow = window.open('', '', 'height=200,width=400');
            printWindow.document.write('<html><head><title>DIV Print DEMO</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(PrintDiv);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        }
</script>
</body>
</html>

Function to print div

<script type="text/javascript">
        function PrintDiv() {
            var PrintDiv = document.getElementById("PrintDiv").innerHTML;
            var printWindow = window.open('', '', 'height=200,width=400');
            printWindow.document.write('<html><head><title>DIV Print DEMO</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(PrintDiv);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        }
</script>

Output