当前位置:编程学习 > asp >>

asp.net中的“按需打印”(打印你需要打印的部分)

有时我们需要对asp.net网页中某些特定部分进行打印,很多人采用CSS样式或特定的打印控件来解决网页定制打印功能。这里采用Javascript样式替换方式进行打印,使网页显示与打印效果相分离。

过程很简单:

首先在asp.net页面中设定开始打印和结束打印的标记,为了确保该标记不在网页浏览时显示,我们采用"<!-- HTML注释 -->"的方式。比如:<!--startprint-->和<!--endprint-->。

接着写相关的JavaScript代码,具体代码见后。

如果不想打印按钮也被打印出来,注意将打印按钮包含在<!--startprint-->和<!--endprint-->之外。

相关代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AppealPrint.aspx.cs" Inherits="Appeal.AppealPrint"

MasterPageFile="MasterPage.master" Title="诉求打印" %>

<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>诉 求 信 息</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">诉求主题:</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">诉求类型:</td>
                        <td  colspan="3">
                              <asp:Label ID="lblAppealCategory" runat="server" Text=""/>
                        </td>
                    </tr>
                    <tr class="tdbg">
                        <td align="right" width="30%" nowrap="nowrap">诉求当前状态:</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">诉求性质:</td>
                        <td style="width: 15%"><asp:Label ID="lblAppealNature" runat="server" Text="Label"/></td>
                        <td align="right" style="width: 25%" nowrap="nowrap">被投诉人姓名:</td>
                        <td style="width: 30%"><asp:Label ID="lblBeAppealName" runat="server" Text="Label"/></td>
                    </tr>


   &nb

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,