Solved

how to print invoices in asp

Posted on 2003-10-28
18
347 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

803 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