Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Apologies as I'm not a web developer but occasionally do the odd job which requires me to create a web form or 2. I have a requirement to print off a report on a webpage and I'm trying to get the print button to work reliably.

The problem I'm having is the second time I'm pressing the print button its not opening a print dialog box for some reason and I can't work out why.

Please see the simplified version of my problem below

C#
protected void PrintAllButton_OnClick(object sender, EventArgs e)
        {
            using (var pdi = new PrintDialog())
            {
                if (pdi.ShowDialog() == DialogResult.OK)
                {
                }
            }
        }


The dialog fires correctly the first time and its in a using statement so should be cleaned up correctly but I'm clearly missing something

Thanks in advance.

What I have tried:

Googling and simplifying the solution to the easiest possible scenario above
Posted
Updated 22-Nov-16 6:05am

1 solution

The code you've shown is executing on the server.

It might appear to work when you debug your code within Visual Studio, but that's only because the server and the client are the same machine in that specific instance.

As soon as you deploy your code to a real server, it will break. At best, you'll get an exception telling you that the current process is not interactive. At worst, the dialog box will appear on the server, where nobody will ever see it, and your code will hang waiting for someone to press a button on an invisible dialog box.

If you want to print the current HTML document, you'll need to use Javascript running on the client, and call the Window.print()[^] method.

If you want to print something other than HTML, then you're going to have to let the user download the file and print it themselves. There is no way to initiate printing an external file from Javascript.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900