locked
Print a page in Webmatrix 2 RRS feed

  • Question

  • User-1621298464 posted

    I'm working with Webmarix 2 and the starter site.  I have created a voucher on a page.  The page includes 4 options to distribute the voucher.  The first is to print. (Required) Is there a way in code to invoke the ctrl-P (Print) process so the user can select their desired printer?

    I have no clue how to print the page within code.

    Thanks

    Dallas in Maryland

     

     

    Friday, June 21, 2013 11:27 AM

Answers

  • User-1205307838 posted

    this should help

    http://www.w3schools.com/jsref/met_win_print.asp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 11:48 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 12:35 PM
  • User-1980594115 posted

    The following may help.  After window.print(), setTimeout will give some time before the page is redirected using document.location.

    If you want the redirected page to "immediately" display, then replace the setTimeout line with the document.location.href line.

    <head>
    <script>
    	function printpage() {
                    window.print();
                    setTimeout("closePrintView()", 3000);
                }
    		function closePrintView() {
    			document.location.href = 'YourNewPage.cshtml';
    		}
    </script>
    </head>
    <body>
    
        The dog is fast. <br />
    
        <input type="button" value="Print this page" onclick="printpage()">
    
    </body>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 3:09 PM

All replies

  • User-1205307838 posted

    this should help

    http://www.w3schools.com/jsref/met_win_print.asp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 11:48 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 12:35 PM
  • User-1621298464 posted

    very helpful ... I can assign the suggested code to the GO button via onclick =. 

    How can I get the user automatically off this page after the printpage() function has completed?

    Friday, June 21, 2013 12:47 PM
  • User-1980594115 posted

    The following may help.  After window.print(), setTimeout will give some time before the page is redirected using document.location.

    If you want the redirected page to "immediately" display, then replace the setTimeout line with the document.location.href line.

    <head>
    <script>
    	function printpage() {
                    window.print();
                    setTimeout("closePrintView()", 3000);
                }
    		function closePrintView() {
    			document.location.href = 'YourNewPage.cshtml';
    		}
    </script>
    </head>
    <body>
    
        The dog is fast. <br />
    
        <input type="button" value="Print this page" onclick="printpage()">
    
    </body>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 3:09 PM
  • User-1621298464 posted

    That all worked perfectly.  I moved the code (function)  to a file named PrintPage.js and loaded it a the top of the page.  Then called the function with an onclick event.  The timeout redirected the user off the current page 3 seconds after the printed dialog appeared.  Perfect.

    Thanks

    Dallas in Maryland.

    @section Scripts { 
     <script src="~/Scripts/PrintPage.js" type="text/javascript"></script>@*PrintPage*@
     }
    
    <div id="to-print-button"> 
                     <button type=button Title="To Printer"
                     onclick="PrintPage()">Go</button>
    </div>

     

    Saturday, June 22, 2013 9:17 AM