How to print contents of Div only

Generally we do not want to create complex report and our clients are not interested in purchasing expensive reporting tools then we can generate simple HTML based reports for them. You just have to place your report inside a< div>... < / div> and you can easily print that. Here is a sample through which you can easily print contents of specified div only:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <script language="javascript" type="text/javascript">
        function printDiv(divID) {
            //Get the HTML of div
            var divElements = document.getElementById(divID).innerHTML; 
            //Get the HTML of whole page
            var oldPage = document.body.innerHTML;
            //Reset the page's HTML with div's HTML only
            document.body.innerHTML = "<html><head><title></title></head><body>" + divElements + "</body>";
            //Print Page
            //Restore orignal HTML
            document.body.innerHTML = oldPage;
            //disable postback on print button
            return false;
    <title>Div Printing Test Application by Zeeshan Umar</title>
    <form runat="server">
    <asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="return printDiv('div_print');" />
    <div id="garbage1">I am not going to be print</div>
    <div id="div_print"><h1 style="color: Red">Only Zeeshan Umar loves Asp.Net will be printed :D</h1></div>
    <div id="garbage2">I am not going to be print</div>

Here is the output of the page when I run the code:-

And when I pressed print button, it hide all the remaining area and only my div's contents are visible which will be printed:-

Feel free to comment if you require any clarification. Happy coding !!!

Article of the Day, XML Serilization Tutorial

On Saturday, Jul 02, 2011, my article on 'XML Serialization Tutorial' was selected as 'Article of the Day' at

I also recommend to all of readers to do share useful articles on Whenever you found an article which is interesting and can be useful for others.