Ý tưởng là đặt các nội dung được in bên trong điều khiển ASP.Net Panel và sau đó in nội dung trong đó.
function PrintPanel[] { var panel = document.getElementById[""]; var printWindow = window.open['', '', 'height=400,width=800']; printWindow.document.write['DIV Contents']; printWindow.document.write['']; printWindow.document.write[panel.innerHTML]; printWindow.document.write['']; printWindow.document.close[]; setTimeout[function [] { printWindow.print[]; }, 500]; return false; } Hello,
This is Mudassar Khan.
Hoping that you are enjoying my articles!
Trong Markup HTML ở trên tôi có control panel pnlContents có nội dung cần in và nút btnPrint trong đó có sự kiện OnClientClick gọi phương thức JavaScript PrintPanel[] để in nội dung của Panel.
There may be times in which your website or application would like to improve the user's experience when printing content. There are a number of possible scenarios: There may be other cases in which you want to manage the printing process, but these are some of the most common scenarios. This article provides tips and techniques for helping your web content print better. Add the following to your Using a print style sheet
tag.
Detecting print requests
Browsers send beforeprint
and afterprint
events to let content
determine when printing may have occurred. You can use this to adjust the user interface presented during printing [such as by displaying or hiding user interface elements during the print process].
Examples
Here are some common examples.
Open and automatically close a popup window when finished
If you want to be able to automatically close a popup window [for example, the printer-friendly version of a document] after the user prints its contents, you can use code like this:
DOCTYPE html>
JavaScript Window Close Example
function popup title[] {
const my_window = window.open[
"",
"mywindow",
"status=1,width=350,height=150"
];
my_window.document.write["Print Me"];
my_window.document.write[''];
my_window.document.write[
"When you print this window, it will close afterward.
"
];
my_window.document.write[""];
}
To try out the afterprint event, click the link below to open
the window to print. You can also try changing the code to use
beforeprint to see the difference.
Open Popup Window
View Live Examples
Print an external page without opening it
If you want to be able to print an external page without opening it, you can utilize a hidden [see:
HTMLIFrameElement], automatically removing it after the user prints its contents. The following is a possible example which will print a file named
externalPage.html
:
DOCTYPE html>
MDN Example
function closePrint[] {
document.body.removeChild[this.__container__];
}
function setPrint[] {
this.contentWindow.__container__ = this;
this.contentWindow.onbeforeunload = closePrint;
this.contentWindow.onafterprint = closePrint;
this.contentWindow.focus[]; // Required for IE
this.contentWindow.print[];
}
function printPage[sURL] {
const hideFrame = document.createElement["iframe"];
hideFrame.onload = setPrint;
hideFrame.style.position = "fixed";
hideFrame.style.right = "0";
hideFrame.style.bottom = "0";
hideFrame.style.width = "0";
hideFrame.style.height = "0";
hideFrame.style.border = "0";
hideFrame.src = sURL;
document.body.appendChild[hideFrame];
}
Print external page!
Note: Older versions of Internet Explorer cannot print the contents of a hidden .