Asp.net 的网络打印 – Javascript 控制打印

---资料来源于Microsfot WebCast.

 

1.       采用Javascript 控制打印:

-          先写的 javascript 的 function

<script language="javascript">

              function preview()

              {

              bdhtml=window.document.body.innerHTML;

              sprnstr="<!--startprint-->";

              eprnstr="<!--endprint-->";

              prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);

              prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));

              window.document.body.innerHTML=prnhtml;

              window.print();

              }

</script>

 

要控制的打印部分用 startprint 和 endprint 来限定区域

- 控制打印部分

<center>本部分以上不被打印</center>

                     <!--startprint-->

                     <div align="center">

                            <asp:DataGrid id="dgShow" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4">

                                   <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>

                                   <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>

                                   <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>

                                   <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>

                                   <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>

                            </asp:DataGrid>

                     </div>

                     <!--endprint-->

<center>本部分以下不被打印</center>

 

-          调用打印部分

<input type="button" name="print" value="预览并打印" onclick="preview()">

 

2.如果要直接打印,又想直接控制到那些不打印出来

       - 先造建一个不打印的样式

<style> @media Print { .Noprn { DISPLAY: none }}</style>

 

-          在希望不打印的控件前面加上不打印的样式

   eg 1: <p class="Noprn">不打印</p>

eg2:<input class="Noprn" type="button" name="print" value="预览并打印" onclick="preview()">

 

-          调用直接打印,并不打印不希望见到的控件

 <input class="Noprn" type="button" onclick="window.print()" value="print">


你可能感兴趣的:(Asp.net 的网络打印 – Javascript 控制打印)