Print the gridview

Source: Internet
Author: User
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 ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.