1.写一个PrintHelper类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Web.SessionState;

namespace PrintPage
{

public class PrintHelper
{
public PrintHelper()
{ }

public static void PrintWebControl(Control control)
{
PrintWebControl(control, string.Empty);
}

public static void PrintWebControl(Control control, string Script)
{
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite);
if (control is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage);
((WebControl)control).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
if (Script != string.Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScipt", Script);
}

HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(control);
pg.RenderControl(htmlWriter);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
}
}

2.创建Default页:
放置一个按钮btnPrint与一个Panel,Panel中是要打印的内容
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PrintPage
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnPrint_Click(object sender, EventArgs e)
{
Session["control"] = Panel1;
ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
}
}
}

创建Print页面:
在form_load事件中调用打印事件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PrintPage
{
public partial class Print : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control control = (Control)Session["control"];
PrintHelper.PrintWebControl(control);
}
}
}

本文转自欢醉博客园博客,原文链接http://www.cnblogs.com/zhangs1986/p/3644018.html如需转载请自行联系原作者

欢醉

asp.net中打印指定控件内容相关推荐

  1. ASP.NET中 Calendar(日期控件)的使用

    ylbtech-ASP.NET-Control-Basic:Calendar(日期控件)的使用 ASP.NET中 Calendar(日期控件)的使用. 1.A,运行效果返回顶部 Calendar(日期 ...

  2. asp.net中显示DataGrid控件列序号的几种方法

    在aps.net中多数据绑定的控件很多,论功能来说,应该属DataGrid最为齐全,但它没有提供现成的显示记录序号的功能,不过我们可以通过它所带的一些参数来间接得到序号,下面来看看怎样得到和显示序号值 ...

  3. ASP.NET中的AdRotator控件即广告控件的使用

    场景 ASP.NET中新建Web网站并部署到IIS上(详细图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/107199 ...

  4. Asp.net中服务端控件事件是如何触发的(笔记)

    Asp.Net 中在客 户端触发服务器端事件分为两种情况: 一.   WebControls中的Button 和HtmlControls中的Type为submit的HtmlInputButton 这两 ...

  5. ASP.NET中的Menu控件的应用

    1.首先建立一个Default.aspx的主页面,再建立一个站点地图:Web.sitemap 2.Default.aspx的主页面的设计 <%@ Page Language="C#&q ...

  6. Asp.net中使用ActiveX控件

    在asp.net项目项目中,使用了一个第三方的没有签名的ActiveX控件,代码如下: <SCRIPT LANGUAGE="JavaScript"> function ...

  7. ASP.NET 中获取html控件的值的方法

    一.知识点: 1.name是用来提交数据的,提供给表单用,可以重复:  2.id则针对文档操作时候用,不能重复.如:document.getElementById():   document.getE ...

  8. 在ASP.NET中使用ListView控件对数据进行显示、分页和排序

    最终效果图如下所示: 使用ListView控件是不需要写代码的,下面我们来看一下ListView的操作流程: 1.首先新建一个ASP窗体 2.然后拖入一个ListView和一个SqlDataSourc ...

  9. ASP.Net中的TreeView控件中对节点的上移和下移操作

    Web中的TreeView中的没有PreNode和NextNode属性. 但它的集合属性中有一个IndexOf属性,从而能够找到它的前一个节点知后一个节点. TreeView中要么只有一个根节点:要么 ...

最新文章

  1. Centos7安装Nginx详细步骤
  2. 网站SEO优化中长尾关键词的特征有哪些?
  3. js 空数组是true还是false
  4. bootstrap 列表--水平定义列表
  5. 一种storyboard+swift实现页面跳转的方法
  6. ​【Python入门】Python数学math模块55个函数详解
  7. C++实现successive approximation渐进法(附完整源码)
  8. 每天一道LeetCode-----括号匹配
  9. 计算机网络的最短帧长公式,每日一题 | CSMA/CD协议的最短帧长公式
  10. TypeScript泛型详解
  11. 浅谈android的am命令
  12. C++面试题,平时面试不可缺少的!
  13. Android之基于message的进程间通信Messenger
  14. Nginx系列(2):10分钟看懂Nginx到底能做什么?
  15. 层次分析法(AHP)原理以及应用
  16. MST703调试群创AT070TN92
  17. 【盘点】2017美国人工智能专业最好的20所大学(附地理位置)Ⅱ
  18. c语言利用公式求sin(x)的近似值,用下列公式计算sin(x)的近似值
  19. 摄影测量(tip1):空间前方交会与空间后方交会
  20. c语言感叹号加一个括号,车上出现一个感叹号一个括号处理方法

热门文章

  1. USACO-Section1.4 Combination Lock (枚举)
  2. Redis未授权访问漏洞记录(端口:6379)
  3. SQLAlchemy ORM教程之一:Create
  4. 6. OD-去除收费软件次数限制,去除退出软件时弹出的广告(比如可执行5次)
  5. 二十二、标志寄存器与栈(代码设计安全,与子程序寄存器安全类似)
  6. C/C++:Windows编程—Inline Hook内联钩子(上)
  7. Linux系统编程:fifo有名管道的使用
  8. CODE[VS] 2824 盗窃-大海的奇迹
  9. shell命令总结一
  10. java byte 梳理