Asp. NET Web page printing (only related content/self-writing function) _ Practical Tips

Source: Internet
Author: User

Last night a friend asked to complete the news in the previous period of the website to add a feature, that is, in each news browsing page, add a print ammonium button. Let the user print a little bit, can print the entire article. A friend at first, call the user to use IE browser print function, but friends said that the printing function is the page irrelevant content also printed, the user just want to print the entire article can.

If you want to change, just do it. Without this experience, find a lot of relevant asp.net print examples on the internet, either too complex or less effective. Finally, the combination of these many examples, rewrite a suitable function.

Here's where to rewrite, first in the article browsing the. aspx page, adding a Div, and adding runat= "server" to include the entire article, such as:

Copy Code code as follows:

<div id= "printcontent" runat= "Server" >
Asp. NET Web page print test <br/>
Content content Content content content content content content contents Content <br/>
Content content Content content content content content content contents Content <br/>
Content content Content content content content content content contents Content <br/>
Content content Content content content content content content contents Content <br/>
Content content Content content content content content content contents Content <br/>
Content content Content content content content content content contents Content <br/>
Content content Content content content content <br/>
</div>

Then, add a print ammonium button:
Copy Code code as follows:

<asp:button id= "Btnprint" runat= "server" text= "print" onclientclick= "return Printarticle ();"/>

See no, the above print ammonium button needs to perform a section of JavaScript code, but also the key part. Insus.net is written below, and this javascript is placed at the end of the. aspx page, which means that the page is finally executed.
Copy Code code as follows:

function Printarticle () {
var pc = document.getElementById ("<%=PrintContent.ClientID%>");
var pw = window.open (', ', ', ' width=500,height=400 ');
Pw.document.write (' Pw.document.write (' Pw.document.write (' <title>asp. NET Web page print Test </title> ');
Pw.document.write (' Pw.document.write (' <body> ');
Pw.document.write (pc.innerhtml);
Pw.document.write (' </body> ');
Pw.document.write (' Pw.document.close ();
settimeout (function () {
Pw.print ();
}, 500);
return false;
}

Finally, an animated demo.:

printout Results :

Also can, but a little is not ideal, is the page header at the end of a line of information, that is highlight part, can cancel it, the answer is yes, refer to the following set method settings.

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.