7 February 2014

Print a page using JavaScript

Below sample code used to print the content which is inside the div tag in the page. 


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Print a page using JavaScript.aspx.cs"
    Inherits="Print_a_page_using_JavaScript" %>

<!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>
    <script type="text/javascript" language="javascript">
        function PrintPage() {
            var page_setup = "location=no,toolbar=yes,scrollbars=yes,width=750, height=750, left=50, top=50";
            var page_vlue = document.getElementById("page_content").innerHTML;
            var docprint = window.open("", "", page_setup);
            docprint.document.open();
            docprint.document.write("<html>");
            docprint.document.write('<body onload="self.print()"><center>');
            docprint.document.write(page_vlue);
            docprint.document.write("</center></body></html>");
            docprint.document.close();
            docprint.focus();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="page_content">
            <table width="100%" border="0">
                <tr>
                    <td>
                        Hiiiiiiiiiiiii this is adi
                        <br />
                        Hiiiiiiiiiiiii this is adi
                        <br />
                        Hiiiiiiiiiiiii this is adi
                        <br />
                        Hiiiiiiiiiiiii this is adi
                        <br />
                        Hiiiiiiiiiiiii this is adi
                        <br />
                    </td>
                </tr>
            </table>
        </div>
        <br />
        <div>
            <a href="javascript:PrintPage()">Print this page</a>
        </div>
    </div>
    </form>
</body>
</html>

 

No comments:

Post a Comment