国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:16:08
字體:
來源:轉載
供稿:網友

過程很簡單:

首先在asp.net頁面中設定開始打印和結束打印的標記,為了確保該標記不在網頁瀏覽時顯示,我們采用"<!-- HTML注釋 -->"的方式。比如:<!--startprint-->和<!--endprint-->。

接著寫相關的JavaScript代碼,具體代碼見后。

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

相關代碼:

復制代碼 代碼如下:


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

MasterPageFile="MasterPage.master" %>

<asp:Content ContentPlaceHolderID="CphNavigation" runat="server">
</asp:Content>
<asp:Content 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 cellspacing="0" cellpadding="2">
    <tbody>
        <tr>
            <td><strong>訴 求 信 息</strong></td>
        </tr>
        <tr>
            <td>
            <table cellspacing="1" cellpadding="2">
                <tbody>
                    <tr>
                         <td nowrap="nowrap">訴求主題:</td>
                         <td colspan="3">
                             <asp:Label runat="server" Text="Label"></asp:Label>
                         </td>
                    </tr>
                    <tr>
                        <td nowrap="nowrap">訴求類型:</td>
                        <td  colspan="3">
                              <asp:Label runat="server" Text=""/>
                        </td>
                    </tr>
                    <tr>
                        <td nowrap="nowrap">訴求當前狀態:</td>
                        <td colspan="3">
                              <asp:Label runat="server" Text="Label"/>
                        </td>
                    </tr>
                    <tr>
                        <td nowrap="nowrap">訴求性質:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                        <td nowrap="nowrap">被投訴人姓名:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                    </tr>


                    <tr>
                        <td nowrap="nowrap">被投訴人地址:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                        <td nowrap="nowrap">投訴人:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                    </tr>


                    <tr>
                        <td nowrap="nowrap">訴求信息是否保密:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                        <td nowrap="nowrap">管理員是否同意公開:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                    </tr>


                    <tr>
                        <td nowrap="nowrap">瀏覽數:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                        <td nowrap="nowrap">回復數:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                    </tr> 

                    <tr>
                        <td nowrap="nowrap">歸檔:</td>
                        <td><asp:Label runat="server" Text=""/></td>
                        <td nowrap="nowrap">訴求提交時間:</td>
                        <td><asp:Label runat="server" Text="Label"/></td>
                    </tr>


                    <tr>
                        <td nowrap="nowrap">訴求所在地域:</td>
                        <td colspan="3"><asp:Label runat="server" Text=""></asp:Label></td>
                    </tr>
                    <tr>
                        <td colspan="4"><asp:Label runat="server" Text=""/></td>
                    </tr>
                    <tr>
                        <td colspan="4" valign="top" nowrap="nowrap">訴求內容:</td>
                    </tr>
                    <tr>
                        <td colspan="4" valign="top">
                            <pe:ExtendedLiteral runat="server" HtmlEncode="false"/>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<!--endprint-->
   <br />
    <div>
        <input type="button" value="打 印…" />
    </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); //放大打印
        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>


觀察上面代碼可以看出,打印時,window.document.body.innerHTML 被重新賦值成設定需要打印的網頁部分的代碼(這里是 prnhtml),然后通過遞歸方式調用setChildNodesByCurrentNode(artibody,size)更改相關子節點的樣式,以達到全部更換相關字體大小及樣式設置的目的。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河间市| 韶关市| 城口县| 皋兰县| 佛冈县| 隆尧县| 惠水县| 天峨县| 甘孜| 旬邑县| 罗江县| 罗源县| 巴楚县| 隆德县| 汕头市| 永泰县| 旺苍县| 华亭县| 鄱阳县| 永川市| 大冶市| 临安市| 偏关县| 铜陵市| 普兰县| 察雅县| 铜梁县| 凤冈县| 德钦县| 宁强县| 旬阳县| 彭山县| 浮山县| 武强县| 贺兰县| 霞浦县| 博罗县| 海原县| 乐平市| 恩平市| 莎车县|