<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <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 window.print(); //Restore orignal HTML document.body.innerHTML = oldPage; //disable postback on print button return false; } </script> <title>Div Printing Test Application by Zeeshan Umar</title> </head> <body> <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> </form> </body>
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 !!!
ziera · 713 weeks ago
Zeeshan Umar · 712 weeks ago
Also there is a chance that in your page you might have some special HTML tag which is causing my script to stop work.
Zeeshan Umar · 712 weeks ago
Also there is a chance that in your page you might have some special HTML tag which is causing my script to stop work.
Eric · 712 weeks ago
Zeeshan Umar · 700 weeks ago
abdo · 675 weeks ago
any way to enable them
Zeeshan Umar · 675 weeks ago
http://www.w3schools.com/tags/tag_noscript.asp
Amrutha · 659 weeks ago
this is very useful
Zeeshan Umar · 658 weeks ago
sanchu · 619 weeks ago
bcoz im trying 4 hours but when i add image control in div , the whole page is printed
please help me asap
Kevin at SiteWizard · 609 weeks ago
This is an interesting approach that I had not thought of before. I can see it being useful in a few scenarios. It is fast and easy to implement for a single report page.
However, the "standard" method of hiding unwanted elements for printing is to use CSS media types. This can be done either by adding a @media block to a stylesheet, or including a special stylesheet to handle printing.
Here is a brief tutorial on the subject: http://www.javascriptkit.com/dhtmltutors/cssmedia...
Once you have a stylesheet for printing, you can hide page elements from the printer like so:
<style type="text/css" media="print">
.no-print, #header, #footer, #navbar { display: none; visibility: hidden; }
</style>
With this approach, all of the elements will appear on-screen, but the selected elements will be hidden when printing. This does not break any JavaScript or embedded objects, so you could even print the contents of JavaScript generated charts and graphs.
BTW, another use of this CSS method is to prevent users from printing completely. By hiding the body contents, you could prevent users from printing subscription-based content.
<style type="text/css" media="print">
#body { display: none; visibility: hidden; }
</style>
Regards,
Kevin
Zeeshan Umar · 555 weeks ago
Hussain · 555 weeks ago
Is there anyway to call the java script function from the code behind .cs file?
Regards
Hussain
Zeeshan Umar · 555 weeks ago
you can use scriptmanager.registerstartupscript function to call javascript from c# code.
Have a look at this:-
http://www.codeproject.com/Questions/443131/Call-...
zasi · 362 weeks ago