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.