This article from: http://blog.csdn.net/fanfengchimo/archive/2007/07/23/1703307.aspx
The original article is as follows:
Today, the company asked for a printing function, that is, to print the content in the gridview, and then checked a lot of Methods online, and finally decided to include the gridview into a div, then, submit the html of the Div to another page and print the page.
Print the specified content:
<HTML>
<Head>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function printpage (){
VaR newwin = Window. Open ('printer ','','');
VaR titlehtml = Document. getelementbyid ("printdiv"). innerhtml;
Newwin.doc ument. Write (titlehtml );
Newwin.doc ument. Location. Reload ();
Newwin. Print ();
Newwin. Close ();
}
</SCRIPT>
</Head>
<Body>
<Div id = "printdiv">
<Table class = "sontable" cellspacing = "0" cellpadding = "0" style = "width: 13%">
<Tr>
<TD style = "width: 700px; Height: 161px">
<Asp: gridview id = "griddata" runat = "server" cellpadding = "3" cellspacing = "0" borderwidth = "1px" backcolor = "lightsteelblue" bordercolor = "white" borderstyle = "None "font-size =" 12px "width =" 543px "Height =" 20px "onrowdatabound =" griddata_rowdatabound ">
<Rowstyle backcolor = "ghostwhite" bordercolor = "#006699"/>
<Selectedrowstyle backcolor = "# 738a9c" font-bold = "true" forecolor = "white" Wrap = "true"/>
<Headerstyle Height = "25px" backcolor = "#006699" font-bold = "true" forecolor = "white" horizontalalign = "center" cssclass = "freezing"/>
</ASP: gridview>
</TD>
</Tr>
</Table>
</Div>
<A href = "javascript:;" onclick = "printpage ()"> Print </a>
</Body>
</Html>
CodeThat's it.
The second method is to hide the labels to be printed on the page, print the labels, and then display all the labels on the page.
The Code is as follows:
Function Printer ()
{
Beforeprint ();
Window. Focus ();
Window. Print ()
Afterprint ();
}
Function beforeprint ()
{
For (I = 0; I <document. All. length; I ++)
{
If (document. All (I). Id. indexof ("div_table _")! =-1) & document. All (I). tagname = "table ")
// "Div_table _" indicates the ID of the tag to be printed.
{
Document. All (I). style. Display = "NONE ";
}
}
}
Function afterprint ()
{
For (I = 0; I <document. All. length; I ++)
{
If (document. All (I). Id. indexof ("div_table _")! =-1) & document. All (I). tagname = "table ")
{
Document. All (I). style. Display = "Block ";
}
}
}
Call printer ().