简体   繁体   English

在ASP.NET C#中使用javascript打印多个面板

[英]Print Multiple panels using javascript in asp.net c#

I have 3 panels in my page, I want each panel to be printed in different page, not in the same page.(I meant Panel 1 in page 1 & panel 2 in page 2 & panel 3 in page 3). 我的页面中有3个面板,我希望每个面板都打印在不同的页面中,而不是在同一页面中。(我的意思是第1页中的Panel 1和第2页中的Panel 2以及第3页中的panel 3)。 what happened now is that the first panel is printed and panel 2 is printed in the same page. 现在发生的是在同一页面上打印了第一块面板,而第二块面板则被打印。 How can I print each one in different seperated page? 如何在不同的单独页面中打印每一个?

Here is the JavaScript code: 这是JavaScript代码:

 <script type = "text/javascript">

       function PrintPanel() {

           var panel = document.getElementById("<%=Panel1.ClientID %>");
           var panel2 = document.getElementById("<%=Panel2.ClientID %>");
           var panel3 = document.getElementById("<%=Panel3.ClientID %>");


           var printWindow = window.open('', '', 'scrollbars=yes,     resizable=yes, width=800, height=500');

          printWindow.document.write('<html><head><title>My Title</title>');

        printWindow.document.write('</head><body >');

        printWindow.document.write(panel.innerHTML);
        printWindow.document.write(panel2.innerHTML);
        printWindow.document.write(panel3.innerHTML);             
        printWindow.document.write('</body></html>');

        printWindow.document.close();

        setTimeout(function () {

            printWindow.print();

        }, 500);

          return false;

    }

</script>

You have to create three separate windows to print the panels like below- 您必须创建三个单独的窗口来打印面板,如下所示:

<script type = "text/javascript">
    function PrintPanel() {
    var panel1 = "<h1>Test1</h1>";
    var panel2 = "<h1>Test2</h1>";
    var panel3 = "<h1>Test3</h1>";

    var printWindow = window.open('', '', 'scrollbars=yes,resizable=yes,width=800,height=500');

    var iframe1 = document.createElement('iframe');
    iframe1.id = 'iframe1';
    iframe1.name = 'iframe1';
    printWindow.document.body.appendChild(iframe1);
    iframe1.contentWindow.document.write(panel1);

    var iframe2 = document.createElement('iframe');
    iframe2.id = 'iframe2';
    iframe2.name = 'iframe2';
    printWindow.document.body.appendChild(iframe2);
    iframe2.contentWindow.document.write(panel2);

    var iframe3 = document.createElement('iframe');
    iframe3.id = 'iframe3';
    iframe3.name = 'iframe3';
    printWindow.document.body.appendChild(iframe3);
    iframe3.contentWindow.document.write(panel3);

    printWindow.document.close();
    setTimeout(function () {
        for (var k = 0; k < printWindow.frames.length; k++) {
            printWindow.frames[k].focus();
            printWindow.frames[k].print();
        }
    }, 500);
    return false;
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM