"Print on demand" in asp.net (print the part you want to print) implementation code

Source: Internet
Author: User
Tags html comment

The process is simple:

First, set the start printing and end printing mark on the asp.net page. To ensure that the mark is not displayed when the web page is browsed, we use "<! -- HTML comment -->. For example: <! -- Startprint --> and <! -- Endprint -->.

Next, write the relevant JavaScript code. For the specific code, see.

If you do not want the print button to be printed, be sure to include the print button in <! -- Startprint --> and <! -- Endprint -->.

Related code:

Copy codeThe Code is 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>
<Tr align = "center">
<Td class = "title"> <strong> Request Information </strong> </td>
</Tr>
<Tr>
<Td style = "height: 25px">
<Table cellspacing = "1" cellpadding = "2" style = "margin: 0px auto; width: 100%; border-collapse: collapse;">
<Tbody>
<Tr class = "tdbg">
<Td width = "30%" align = "right" nowrap = "nowrap"> Appeal subject: </td>
<Td colspan = "3">
<Asp: Label ID = "lblEditTitle" runat = "server" Text = "Label"> </asp: Label>
</Td>
</Tr>
<Tr class = "tdbg">
<Td align = "right" width = "30%" nowrap = "nowrap"> appeal type: </td>
<Td colspan = "3">
<Asp: Label ID = "lblAppealCategory" runat = "server" Text = ""/>
</Td>
</Tr>
<Tr class = "tdbg">
<Td align = "right" width = "30%" nowrap = "nowrap"> current appeal status: </td>
<Td colspan = "3">
<Asp: Label ID = "LblStatus" runat = "server" Text = "Label"/>
</Td>
</Tr>
<Tr class = "tdbg">
<Td align = "right" style = "width: 30%" nowrap = "nowrap"> nature of appeal: </td>
<Td style = "width: 15%"> <asp: Label ID = "lblAppealNature" runat = "server" Text = "Label"/> </td>
<Td align = "right" style = "width: 25%" nowrap = "nowrap"> name of the respondent: </td>
<Td style = "width: 30%"> <asp: Label ID = "lblBeAppealName" runat = "server" Text = "Label"/> </td>
</Tr>

<Tr class = "tdbg">
<Td align = "right" style = "width: 30%" nowrap = "nowrap"> respondent address: </td>
<Td style = "width: 15%"> <asp: Label ID = "lblBeAppealAddress" runat = "server" Text = "Label"/> </td>
<Td align = "right" style = "width: 25%" nowrap = "nowrap"> Complainant: </td>
<Td style = "width: 30%"> <asp: Label ID = "lblUserName" runat = "server" Text = "Label"/> </td>
</Tr>

<Tr class = "tdbg">
<Td align = "right" style = "width: 30%" nowrap = "nowrap"> whether the appeal information is kept confidential: </td>
<Td style = "width: 15%"> <asp: Label ID = "lblIsSecret" runat = "server" Text = "Label"/> </td>
<Td align = "right" style = "width: 25%" nowrap = "nowrap"> does the Administrator agree to make public: </td>
<Td style = "width: 30%" align = "right"> <asp: Label ID = "lblAdminIsPublic" runat = "server" Text = "Label"/> </td>
</Tr>

<Tr class = "tdbg">
<Td align = "right" style = "width: 30%" nowrap = "nowrap"> browser count: </td>
<Td style = "width: 15%"> <asp: Label ID = "lblHits" runat = "server" Text = "Label"/> </td>
<Td align = "right" style = "width: 25%" nowrap = "nowrap"> replies: </td>
<Td style = "width: 30%"> <asp: Label ID = "lblReply" runat = "server" Text = "Label"/> </td>
</Tr>

<Tr class = "tdbg">
<Td align = "right" style = "width: 30%" nowrap = "nowrap"> archive: </td>
<Td style = "width: 15%"> <asp: Label ID = "lblCategory" runat = "server" Text = ""/> </td>
<Td align = "right" style = "width: 25%" nowrap = "nowrap"> request submission time: </td>
<Td style = "width: 30%"> <asp: Label ID = "lblCreateTime" runat = "server" Text = "Label"/> </td>
</Tr>

<Tr class = "tdbg">
<Td align = "right" style = "width: 30%" nowrap = "nowrap"> region: </td>
<Td colspan = "3"> <asp: Label ID = "lblRegion" runat = "server" Text = ""> </asp: Label> </td>
</Tr>
<Tr class = "tdbg">
<Td colspan = "4"> <asp: Label ID = "Label2" runat = "server" Text = ""/> </td>
</Tr>
<Tr class = "tdbg">
<Td colspan = "4" align = "left" valign = "top" nowrap = "nowrap"> appeal content: </td>
</Tr>
<Tr class = "tdbg">
<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 = commandid Doc ument. body. innerHTML;
Sprnstr = "<! -- Startprint --> ";
Eprnstr = "<! -- Endprint --> ";
Prnhtml = bdhtml. substr (bdhtml. indexOf (sprnstr) + 17 );
Prnhtml = prnhtml. substring (0, prnhtml. indexOf (eprnstr ));
Too many Doc ument. body. innerHTML = prnhtml;
Var obj = invalid argument Doc ument. body;
DoZoom (obj, 18); // zoom in and print
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 = ' _ GB2312,, "Times New Roman", Georgia, serif ';
ArtibodyChild. style. fontWeight = 900;
If (artibodyChild. childNodes. length> 0 ){
SetChildNodesByCurrentNode (artibodyChild, size );
}
}
}
}

-->

</Script>
</Asp: Content>

Observe the runtime code to see that when printing, the runtime Doc ument. body. innerHTML is assigned a value again to set the code (prnhtml) of the page to be printed, and then call setChildNodesByCurrentNode (artibody, size) recursively to change the style of the child node, to change the font size and style settings.

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.