how to print invoices in asp

i want to print the selected part of an asp page using code
while the rest of the page is diplayed

thanks in advance
AeldinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
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
gdeanCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhagyeshtCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gdeanCommented:
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
bhagyeshtCommented:
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
gdeanCommented:
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
bhagyeshtCommented:
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
gdeanCommented:
yeah, I can understand where you're coming from.  Its easy enough to do.
0
gdeanCommented:
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
gdeanCommented:
opps

var target = getElementById('table1')

should of course be

var target = getElementById('table1');
0
bhagyeshtCommented:
Aeldin: some feedback please?
0
gdeanCommented:
>> 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
gdeanCommented:
I mean

var target = getElementById('table1');
0
bhagyeshtCommented:
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
gdeanCommented:
ahh i see that now
0
gdeanCommented:
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
bhagyeshtCommented:
looks like Aeldin has done the vanishing act. so more job for the cleanup fellas
0
AeldinAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.