ASP.NET通过IE直接打印

⒈通过Window.Print()直接打印 

 

<% @ Page language="c#" Codebehind="WPrint.aspx.cs" AutoEventWireup="false" Inherits="ASPNETPrint._01WindowPrint.WPrint"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
    
< HEAD >
        
< title > WebForm1 </ title >
        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio 7.0" >
        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
        
< 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 >
    
</ HEAD >
    
< body  MS_POSITIONING ="GridLayout" >
        
< form  id ="WebForm1"  method ="post"  runat ="server" >
            
< 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 >
            
< div  align ="center" >
                
< input  type ="button"  name ="print"  value ="预览并打印"  onclick ="preview()" >
            
</ div >
            
< style >  @media Print { .Noprn { DISPLAY: none }}
            
</ style >
            
< class ="Noprn" > 不打印 </ p >
            
< table  id ="datagrid" >
                
< tr >
                    
< td > 打印 </ td >
                
</ tr >
            
</ table >
            
< input  class ="Noprn"  type ="button"  onclick ="window.print()"  value ="print" >
        
</ form >
    
</ body >
</ HTML >

上面的preview()方法是用来打印的方法,如果要打印网页中的某一部分,只要把这一部分标记为<!--startprint--><!---endprint-->中就可以了

 

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

上面的样式用来控制打印时是否显示,如果不要显示,只要把他的class设置为Noprn即可.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值