How to print receipt in asp.net?

I have asp.net webpage that, after certain database transaction is done, needs to print the receipt. I accomplish it by the following:

        Response.Write("<script language='javascript'>{window.open('receipt.aspx');}</script>")

The page receipt.aspx needs to open, print itself, and close. This is accomplised by the following javascript in receipt.aspx:

    <script type="text/javascript">
    <!--
        window.print();
        window.close();
    //-->
    </script>


It all works OK in Firefox, but in IE8 the user sees a prompt "The script is trying to close the page, do you want to allow it?", and the page does not close even if the user answers "yes". This is probably because of the print dialog, because if I remove window.print, the page does close. In Chrome it closes the window without any print.

It would be nice to (1) remove the prompt (2) print to the default printer without print dialog (3) close the window automatically. I.e. to make IE8 act like Firefox.

Or perhaps there's some better way to do it, i.e. print several lines on client printer?
LVL 40
Vadim RappAsked:
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.

Paul MacDonaldDirector, Information SystemsCommented:
1) You can change security settings in IE (Tools - > Internet Options, Security Tab) to eliminate the prompt

2) You can't, and I see this question posted over and over.  Imagine a web page that can just spit stuff to your printer whenever it wants!

3) See (1)
0
Vadim RappAuthor Commented:
> Imagine a web page that can just spit stuff to your printer whenever it wants!

I think, the page that is in trusted zone is trusted to do whatever it wants.

Firefox apparently does not see that as a problem, either.

>  You can change security settings

Can you tell, which security setting will do that?
0
RobOwner (Aidellio)Commented:
I think part of the problem is that the page hasn't loaded fully when you're calling this javascript.

<script>
window.onload = function() {
    window.print();
}
</script>

Open in new window


I would then have a link for the user to close the page.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Vadim RappAuthor Commented:
Printing without print dialog and closing without the prompt was accomplished by the following code:
<script type="text/vbscript" language="vbscript">
   Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
   End Sub
   document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script> 

<script type="text/javascript">
    window.onload = function() {
        do {
            window.print();
            var r = confirm("Send to printer. Reprint?");
            if (r == false) {break;}
           }
        while (1 == 1);
        window.open('', '_self', ''); // this is how to avoid IE's prompt "the page you are viewing is trying to close this window" on the next statement
        window.close();
    }
</script>

Open in new window

Comments:
1. printing is using an ActiveX object that is IE itself, no installation of anything else is necessary.
2. security settings have no effect on this.
3. the trick  with window.open('', '_self', '') works by tricking IE to think that the page that is being closed was opened by IE itself. Then it does not show the warning.
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
RobOwner (Aidellio)Commented:
So your code works in Chrome as well now?
0
Vadim RappAuthor Commented:
The objective was to make it work in IE8.
0
RobOwner (Aidellio)Commented:
Not disputing that. You said "In Chrome it closes the window without any print", just curious to know if it fixed that issue. Given this will be saved for future reference, I'm just clarifying the solution.
0
Vadim RappAuthor Commented:
This solution is based on intrinsic Windows ActiveX, implemented by ieframe.dll, so since Chrome does not support ActiveX, it won't work in Chrome.

I think (but did not try) that the problem in Chrome may be related to Chrome's custom dialog for print, instead of the standard system Print dialog. If so, then I'd try to disable that dialog and see if the problem is resolved. In the past it was possible to disable it by using about:flags, but that option has disappeared from the recent versions. According to http://productforums.google.com/forum/#!topic/chrome/br-SKEDR8dc , it's still possible by launching Chrome with commandline parameter -args --disable-print-preview. All that would be a matter of quite a research.
0
Vadim RappAuthor Commented:
That's what worked.
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
ASP.NET

From novice to tech pro — start learning today.