Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to print invoices in asp

Posted on 2003-10-28
18
353 Views
Last Modified: 2010-04-17
i want to print the selected part of an asp page using code
while the rest of the page is diplayed

thanks in advance
0
Comment
Question by:Aeldin
18 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 9633262
The trick will be popup another formatted page where contains only the part you want to print. And in the body onload event of this popup page, try show the print dialog by adding window.print();
0
 

Accepted Solution

by:
gdean earned 20 total points
ID: 9633411
That is one way of doing.  I prefer to use css and hide whatever I don't want printing.  There are a couple of ways of doing this.  The @media tag is supposed to allow you to define css rules for different media in one style sheet.  I however have found that some browsers, inlcuding IE5 leak the print styles into the screen styles.

I would recommend using a seperate css file for your print styles and just including the the below tag:

<link rel="stylesheet" media="print" href="INSERT YOUR URL" type="text/css">

You could try defining the styles in the asp page as well, however I believe this will still leak to the screen styles in some browsers.

The above tag is the safest way, IMO.

If your not familar with css, its a really simple concept/langauge:

Heres a link about the @media tag

http://www.w3schools.com/css/css_mediatypes.asp

Heres one with a ton of examples

http://www.w3schools.com/css/css_examples.asp
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9640539
Assuming you want to print the data between the table tag having id and name "table1"
the following will help you achive this.

<script>
var newwindow=window.open("about:blank");
newWindow.document.write (window.table1.innerHTML);
newWindow.print
newWindow.close
</script>

the table must be before this script tag.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Expert Comment

by:gdean
ID: 9640968
If you want to do it this way Id recommend adding a link to the table below/above
such as:

<a href="javascript:sendPrint();">print</a>

then I'd encapsulate the printing code in the sendPrint function

function sendPrint()
{
var newwindow=window.open("about:blank");
newWindow.document.write (window.table1.innerHTML);
newWindow.print
newWindow.close
}

This way the table wont print everytime the page is loaded
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9641081
gdean: that depends on the asker maybe he wants to do it everytime or might be an asp page to print it specifically. yes you can do it like you said. I just guided the asker.
if its for netscape dont use window.table1 but getelementsbyname
0
 

Expert Comment

by:gdean
ID: 9641126
use either getElementsByName() or getElementById()

asker: it is case sensitive

also change these lines

>>var newwindow=window.open("about:blank");
>>newWindow.print
>>newWindow.close

to

var newwindow=window.open('about:blank');
newWindow.print();
newWindow.close();

0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9641221
gdean: yes you are right. i just wrote the code from memory. this shows signs of aging ;-) been working a lot on vbscript instead of javascript.
0
 

Expert Comment

by:gdean
ID: 9643776
yeah, I can understand where you're coming from.  Its easy enough to do.
0
 

Expert Comment

by:gdean
ID: 9648065
opps


you'll also need to close the document so you can print

function sendPrint()
{
var target = getElementById('table1') // <-- use whatever your table's id is
var newwindow=window.open('about:blank');
newWindow.document.write (target .innerHTML);
newWindow.document.close();
newWindow.print();
newWindow.close();
}

that should do it here is a link with someone that had a simalr question

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20702779.html
0
 

Expert Comment

by:gdean
ID: 9648067
opps

var target = getElementById('table1')

should of course be

var target = getElementById('table1');
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9648093
Aeldin: some feedback please?
0
 

Expert Comment

by:gdean
ID: 9650300
>> that depends on the asker maybe he wants to do it everytime >> or might be an asp page to print it specifically.

true, however, its not a very good idea to have free code such as this executing while the page loads.

Consider, what would happen if the tbale contained an image, or later down the road it was modified to contain an image.  With the code floating, there is no way of knowing what might happen.  Even if it did work its not the right way to write javascript.

If you wanted the table to print everytime the page loads the code should still be wrapped in a function and called from the onLoad event.

here's an example of how this could be done.

<body onLoad="javascript:sendPrint();">

function sendPrint()
{
var target = getElementById('table1')
newwindow=window.open('about:blank');
newWindow.document.write (target .innerHTML);
newWindow.document.close();
newWindow.print();
newWindow.close();
}


0
 

Expert Comment

by:gdean
ID: 9650316
I mean

var target = getElementById('table1');
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9655458
gdean: body's onload wont work as Aeldin wants to print while the page is loading 'maybe some heavy download or could be even an advt'.
Aeldin: Can we have some feedback?
0
 

Expert Comment

by:gdean
ID: 9655925
ahh i see that now
0
 

Expert Comment

by:gdean
ID: 9656910
Aeldin:

If this was your intention, printing while the page loads, its not a good idea.  You should be using the onLoad event to trigger the printing.

ARE YOU THERE?
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9656928
looks like Aeldin has done the vanishing act. so more job for the cleanup fellas
0
 

Author Comment

by:Aeldin
ID: 9665633
Thanks for all , sorry for being late for replying because it's my first time to use this great website ,
thanks again for all
aeldin
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question