我有一个如下所示的javascript函数正常工作。注意 - 我确实更改了该行(来自互联网示例)
WinPrint.document.write(printContent.innerHTML);
到
WinPrint.document.write(printContent.outerHTML);
我不认为我应该这样做但是使用innerhtml网格根本没有格式化但是使用值outerhtml它很好......
总结
感谢, 达莫
的Javascript
<script type="text/javascript">
function PrintGridData(GridToPrint) {
var printContent = document.getElementById(GridToPrint);
var windowUrl = 'about:blank';
var UserLoggedIn = $("#lblUser").text()
var now = new Date();
var strDateTime = [[AddZero(now.getDate()), AddZero(now.getMonth() + 1), now.getFullYear()].join("/"), [AddZero(now.getHours()), AddZero(now.getMinutes())].join(":"), now.getHours() >= 12 ? "PM" : "AM"].join(" ");
var windowName = 'Report';
var AuditPrintDetail = 'Report ' + UserLoggedIn + " " + strDateTime;
var WinPrint = window.open(windowUrl, windowName, 'left=300,top=300,right=500,bottom=500,width=1000,height=500');
WinPrint.document.write('<' + 'html' + '><head><link href="cssreference" rel="stylesheet" type="text/css" /><link href="assets/css/Main.css" rel="stylesheet" type="text/css" /> <title>' + AuditPrintDetail + '</title> </head><' + 'body style="background:none !important"' + '>');
WinPrint.document.write(printContent.outerHTML);
WinPrint.document.write('<' + '/body' + '><' + '/html' + '>');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
}
function AddZero(num) {
return (num >= 0 && num < 10) ? "0" + num : num + "";
}
答案 0 :(得分:1)
function printItn() {
//you can put your contentID which is you want to print.
var printContent = document.getElementById('<%= pnlForm.ClientID %>');
var windowUrl = 'about:blank';
var uniqueName = new Date();
var windowName = 'Print' + uniqueName.getTime();
// you should add all css refrence for your Gridview. something like.
var WinPrint= window.open(windowUrl,windowName,'left=300,top=300,right=500,bottom=500,width=1000,height=500');WinPrint.document.write('<'+'html'+'><head><link href="cssreference" rel="stylesheet" type="text/css" /><link href="gridviewcssrefrence" rel="stylesheet" type="text/css" /></head><'+'body style="background:none !important"'+'>');
WinPrint.document.write(printContent.innerHTML);
WinPrint.document.write('<'+'/body'+'><'+'/html'+'>');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
}
从这里引用它...... click to view discussion。
并查看示例代码javascript to print gridview data from client-side的完整示例。