⒈通过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 >
< p class ="Noprn" > 不打印 </ p >
< table id ="datagrid" >
< tr >
< td > 打印 </ td >
</ tr >
</ table >
< input class ="Noprn" type ="button" onclick ="window.print()" value ="print" >
</ form >
</ body >
</ HTML >
<! 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 >
< p 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 >
</ style >
上面的样式用来控制打印时是否显示,如果不要显示,只要把他的class设置为Noprn即可.