0

I have a button that prints

<input type = "button" onclick = "printDiv ('printableArea')" class = "button1" value = "Print" />


<script>
    function printDiv (divName) {
        var printContents = document.getElementById (divName) .innerHTML;
        var originalContents = document.body.innerHTML;

        document.body.innerHTML = printContents;

        window.print ();

        document.body.innerHTML = originalContents;
    }
</script>

And it sends to print operation.

The customer currently has the option to print or cancel.

Is there a way to access code behind in case the customer presses a print button? (I want to change in my database that the client has already printed ...)

Regards

R-S
  • 151
  • 10

2 Answers2

1

Is there a way to access code behind in case the customer presses a print button?

Please note that we can not directly detect if user clicked the 'Print' or 'Cancel' button in printing dialog via JS code from a html page.

Similar issue discussed in this SO thread: How to capture the click event on the default print menu called by Javascript window.print()?

As a workaround, you can try to show a prompt dialog to confirm if the printing is complete and then make request to backend to update database once the afterprint event is raised, like below.

window.onafterprint = function () {
    var message = "Have you printed the page(s)?";
    var result = window.prompt(message,"yes");
    if (result=="yes") {
        //...
        //make ajax request to backend 
        //...
    }
};
Fei Han
  • 26,415
  • 1
  • 30
  • 41
0

You can add a GET request to an MVC Action like (in jQuery):


...
document.body.innerHTML = printContents;

var clientId = $("#clientId").val();
$.get("@Url.Action("SavePrinted", "Client")", { clientId: clientId });

window.print();
...
Nicola Biada
  • 2,325
  • 1
  • 8
  • 22