asp.net "On-demand Printing" (print the part you need to print) to implement code _ practical tips

Source: Internet
Author: User

The process is simple:

First in the ASP.net page, set the mark to start printing and finish printing, in order to ensure that the tag does not appear when browsing the page, we adopt the "<!--HTML annotation-->" approach. such as:<!--startprint--> and <!--endprint-->.

Then write the relevant JavaScript code, the specific code to see after.

If you do not want the Print button to be printed, note that the Print button is included outside the <!--startprint--> and <!--endprint-->.

Related code:

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "AppealPrint.aspx.cs" inherits= "Appeal.appealprint"

Masterpagefile= "Masterpage.master" title= "appeal print"%>

<asp:content id= "Content1" contentplaceholderid= "cphnavigation" runat= "Server" >
</asp:Content>
<asp:content id= "Content2" contentplaceholderid= "cphcontent" runat= "Server" >
<!--startprint-->
<style type= "Text/css" >
body,table{
font-size:12px;
}
table{
table-layout:fixed;
Empty-cells:show;
Border-collapse:collapse;
margin:0 Auto;
}
td{
height:20px;
}
h1,h2,h3{
font-size:12px;
margin:0;
padding:0;
}


table{
border:1px solid #cad9ea;
Color: #666;
}
Table th{
height:30px;
}
Table Td,table th{
border:1px solid #cad9ea;
padding:0 1em 0;
}
Table tr{
Background-color: #f5fafe;
}
</style>
<table class= "Border" cellspacing= "0" cellpadding= "2" style= "margin:0px auto; width:98%;border:1px solid;border-collapse:collapse; "id=" Tabbtnprint ">
<tbody>
&LT;TR align= "center" >
&LT;TD class= "title" ><strong> Appeal information </strong></td>
</tr>
<tr>
&LT;TD style= "height:25px" >
<table cellspacing= "1" cellpadding= "2" style= "margin:0px auto; Width:100%;border-collapse:collapse; " >
<tbody>
<tr class= "tdbg" >
&LT;TD width= "30%" align= "right" nowrap= "nowrap" > Appeal theme:</td>
&LT;TD colspan= "3" >
<asp:label id= "Lbledittitle" runat= "Server" text= "Label" ></asp:Label>
</td>
</tr>
<tr class= "tdbg" >
&LT;TD align= "Right" width= "30%" nowrap= "nowrap" > Appeal type:</td>
&LT;TD colspan= "3" >
<asp:label id= "lblappealcategory" runat= "Server" text= ""/>
</td>
</tr>
<tr class= "tdbg" >
&LT;TD align= "Right" width= "30%" nowrap= "nowrap" > Appeal current status:</td>
&LT;TD colspan= "3" >
<asp:label id= "lblstatus" runat= "Server" text= "Label"/>
</td>
</tr>
<tr class= "tdbg" >
&LT;TD align= "Right" style= "width:30%" nowrap= "nowrap" > Appeal Nature:</td>
&LT;TD style= "width:15%" ><asp:label id= "lblappealnature" runat= "Server" text= "Label"/></td>
&LT;TD align= "Right" style= "width:25%" nowrap= "nowrap" > The name of the complainant:</td>
&LT;TD style= "width:30%" ><asp:label id= "Lblbeappealname" runat= "Server" text= "Label"/></td>
</tr>


<tr class= "tdbg" >
&LT;TD align= "Right" style= "width:30%" nowrap= "nowrap" > The complainant's address:</td>
&LT;TD style= "width:15%" ><asp:label id= "lblbeappealaddress" runat= "Server" text= "Label"/></td>
&LT;TD align= "Right" style= "width:25%" nowrap= "nowrap" > Complainant:</td>
&LT;TD style= "width:30%" ><asp:label id= "Lblusername" runat= "Server" text= "Label"/></td>
</tr>


<tr class= "tdbg" >
&LT;TD align= "Right" style= "width:30%" nowrap= "nowrap" > Appeal information is confidential:</td>
&LT;TD style= "width:15%" ><asp:label id= "Lblissecret" runat= "Server" text= "Label"/></td>
&LT;TD align= "Right" style= "width:25%" nowrap= "nowrap" > Admin agree to open:</td>
&LT;TD style= "width:30%" align= "right" ><asp:label id= "lbladminispublic" runat= "Server" text= "Label"/></ Td>
</tr>


<tr class= "tdbg" >
&LT;TD align= "Right" style= "width:30%" nowrap= "nowrap" > Browse number:</td>
&LT;TD style= "width:15%" ><asp:label id= "lblhits" runat= "Server" text= "Label"/></td>
&LT;TD align= "Right" style= "width:25%" nowrap= "nowrap" > Reply number:</td>
&LT;TD style= "width:30%" ><asp:label id= "lblreply" runat= "Server" text= "Label"/></td>
</tr>

<tr class= "tdbg" >
&LT;TD align= "Right" style= "width:30%" nowrap= "nowrap" > Archive:</td>
&LT;TD style= "width:15%" ><asp:label id= "lblcategory" runat= "Server" text= ""/></td>
&LT;TD align= "Right" style= "width:25%" nowrap= "nowrap" > Appeal submission Time:</td>
&LT;TD style= "width:30%" ><asp:label id= "Lblcreatetime" runat= "Server" text= "Label"/></td>
</tr>


<tr class= "tdbg" >
&LT;TD align= "Right" style= "width:30%" nowrap= "nowrap" > Appeal region:</td>
&LT;TD colspan= "3" ><asp:label id= "lblregion" runat= "Server" text= "" ></asp:Label></td>
</tr>
<tr class= "tdbg" >
&LT;TD colspan= "4" ><asp:label id= "Label2" runat= "Server" text= ""/></td>
</tr>
<tr class= "tdbg" >
&LT;TD colspan= "4" align= "left" valign= "the Top" nowrap= "nowrap" > Appeal content:</td>
</tr>
<tr class= "tdbg" >
&LT;TD colspan= "4" align= "left" valign= "Top" >
<pe:extendedliteral id= "ltreditcontent" runat= "Server" htmlencode= "false"/>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<!--endprint-->
<br/>
<div id= "Divbtnprint" style= "Text-align:center" >
<input id= "btnprint" type= button "value=" Print ... "onclick=" doprint (); "/>
</div>
<br/>
<script type= "Text/javascript" >

<!--

     function Doprint () {
        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;
        var obj = window.document.body;
        dozoom (obj, 18);//Enlarge printing
         Window.print ();
   }

function Dozoom (artibody, size) {
if (!artibody)
{
Return
}
Setchildnodesbycurrentnode (artibody,size);
}

function Setchildnodesbycurrentnode (node,size) {
for (Var i=0 i < node.childNodes.length; i++) {
var artibodychild = node.childnodes[i];
if (Artibodychild.nodetype = = 1) {
Artibodychild.classname= "";
artibodyChild.style.fontSize = size + ' px ';
artibodyChild.style.fontFamily = ' Imitation _gb2312, song Body, ' Times New Roman ', Georgia,serif ';
ArtibodyChild.style.fontWeight = 900;
if (artibodychild.childnodes.length>0) {
Setchildnodesbycurrentnode (artibodychild,size);
}
}
}
}

-->

</script>
</asp:Content>


Looking at the above code, you can see that when printing, the Window.document.body.innerHTML is assigned to the code that sets the portion of the page that needs to be printed (this is prnhtml), Setchildnodesbycurrentnode (artibody,size) is then invoked recursively to change the style of the associated child nodes to replace the associated font size and style settings for all.

Related Article

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.