列印網頁中的 GridView

來源:互聯網
上載者:User

本文介紹列印網頁中某個GirdView的一種方法。主要思路是將GridView放在一個DIV中,然後將該DIV顯示在新的網頁中,並用window.print()方法來列印新開啟的網頁。

先將GridView放在DIV中,此DIV就是列印範圍。

代碼

1 <div id="PrintArea">
2  <asp:GridView ClientIDMode="Static" ID="GV" runat="server" AutoGenerateColumns="False">
3     <Columns>
4         <asp:BoundField DataField="ID" HeaderText="ID" />
5         <asp:BoundField DataField="Name" HeaderText="Name" />
6     </Columns>
7 </asp:GridView>        
8 </div>

 

然後寫一個Javascript函數,它將開啟一個新網頁,並將指定DIV中的內容寫入該網頁,再列印該網頁,列印完畢後關閉該網頁。

代碼

 1 function PrintGridView() {
 2     // 開啟一個新網頁
 3     var newwindow = window.open('');
 4 
 5     // 將指定DIV中的內容寫入該網頁
 6     newwindow.document.write(document.getElementById('PrintArea').innerHTML);
 7     newwindow.document.close();
 8 
 9     // 列印該網頁
10     newwindow.focus();
11     newwindow.print();
12 
13     // 列印完畢後關閉該視窗
14     newwindow.close();
15     return;
16 }

 

調用該函數即可列印PrintArea中的GridView。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.