在asp.net中使用css打印列表视图,使用javascript

Printing listview in asp.net with css, using javascript

本文关键字:视图 列表 使用 javascript 打印 css net asp      更新时间:2023-09-26

打印时没有使用css类但是当使用

程序时,我看到了css类
        <script type="text/javascript">
        function printDiv() {
            var divToPrint = document.getElementById('DivIdToPrint');
            var newWin = window.open('', 'Print-Window');
            //newWin.document.open();
            newWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
            newWin.document.close();
            setTimeout(function() { newWin.close(); }, 10);
        }
    </script>

 <div id="DivIdToPrint" class="ListadoLicencias">
          <asp:Listview>
      </div>
                 <br />
  <asp:Button runat="server" ID="cmdImprimir" Text="Imprimir" OnClientClick="printDiv();"  />

我认为问题是innerHTML只是做它所说的:它需要内部 HTML。你需要做一些类似

的事情
newWin.document.write(
    '<html><body><div id="DivIdToPrint" class="ListadoLicencias">'
        + divToPrint.innerHTML
        + '</div></body></html>'
);

或者取父元素的innerHTML

要应用你的样式表,你必须将你在弹出函数中编写的HTML扩展到

<html><head><link href="style.css" rel="stylesheet"/></head><!-- ... --></html>