ASP.NET 打印

通过缓存Cache,来缓存Gridview里数据,在需要打印时就可以直接打印了。
打印界面:
2009060907104518.jpg
缓存类:

ContractedBlock.gif ExpandedBlockStart.gif Code
public class CacheData
{
    
private static CacheData instance;

    
private CacheData() { }

    
public static CacheData GetCache()
    {
        
if (instance == null)
        {
            instance 
= new CacheData();
        }
        
return instance;
    }
    
public void CacheDataSet(DataTable dt) //设置要缓存的数据
    {
        HttpRuntime.Cache.Insert(
"printDT", dt, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
    }
    
public DataTable GetCacheData()  //返回缓存的数据
    {
        
if ((DataTable)HttpRuntime.Cache["printDT"!= null)
            
return (DataTable)HttpRuntime.Cache["printDT"];
        
else return null;
    }
}

 

 在数据绑定时就 CacheData.GetCache().CacheDataSet(dt); //缓存数据,用以打印
打印界面代码

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Printer.aspx.cs" Inherits="DataManage_AppComFun_DeptManage_Printer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>打印设置</title>
    
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    
<!--media=print 这个属性可以在打印时有效-->
    
<style media=print type="text/css">
        .Noprint{display:none;}
        .PageNext{page
-break-after: always;}
    
</style>

    
<style type="text/css">
    .tdp
    {
        border
-bottom: 1 solid #000000;
        border
-left:  1 solid #000000;
        border
-right:  0 solid #ffffff;
        border
-top: 0 solid #ffffff;
    }
    .tabp
    {
        border
-color: #000000 #000000 #000000 #000000;
        border
-style: solid;
        border
-top-width: 2px;
        border
-right-width: 2px;
        border
-bottom-width: 1px;
        border
-left-width: 1px;
    }
    .NOPRINT
    {
        font
-family: "宋体";
        font
-size: 9pt;
    }
</style>
</head>
<body style="text-align:center">
    
<center class="Noprint" >
          
<object  id="WebBrowser"  classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"  height="0"  width="0">
          
</object>
          
<input  type="button"  value="打印" onclick="document.all.WebBrowser.ExecWB(6,1)" />
          
<input  type="button"  value="直接打印" onclick="document.all.WebBrowser.ExecWB(6,6)" />
          
<input  type="button"  value="页面设置" onclick="document.all.WebBrowser.ExecWB(8,1)" /> 
          
<input  type="button"  value="打印预览"  onclick="document.all.WebBrowser.ExecWB(7,1)" />
         
<hr align="center" width="90%" size="1" noshade style="height: 1px">
    
</center>
    
  
<form id="form1" runat="server">
    
<div  style="text-align:center;margion:0px">
        
<asp:GridView ID="gvPrint" runat="server">
        
</asp:GridView>
    
</div>
  
</form>
</body>
</html>

后代代码:

ContractedBlock.gif ExpandedBlockStart.gif Code
if (!IsPostBack)
        {
            
if (CacheData.GetCache().GetCacheData()!=null)
            {
                
this.gvPrint.DataSource = CacheData.GetCache().GetCacheData();
                
this.gvPrint.DataBind();
            }
            
else Alert("没有要打印的数据,不能打印!");
        }

 

转载于:https://www.cnblogs.com/lhking/archive/2009/06/09/1499265.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值