惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
SecWiki News
SecWiki News
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
腾讯CDC
Scott Helme
Scott Helme
I
InfoQ
Cyberwarzone
Cyberwarzone
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Security Latest
Security Latest
The Register - Security
The Register - Security
Project Zero
Project Zero
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
C
Cisco Blogs
L
LINUX DO - 热门话题
P
Privacy International News Feed
IT之家
IT之家
U
Unit 42
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Palo Alto Networks Blog
F
Full Disclosure
宝玉的分享
宝玉的分享
Simon Willison's Weblog
Simon Willison's Weblog
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
PCI Perspectives
PCI Perspectives
Hugging Face - Blog
Hugging Face - Blog
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Recent Announcements
Recent Announcements
Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog

博客园 - GO-NET

jQuery选择器总结(转) Js操作Excel常用方法 DataView不能按中文排序问题解决 浮动层居中的对话框效果演示 javascript获得图片的大小 长宽 查找父元素和子元素 ajax form提交 javascript设置分页 url参数为page 网页鼠标提示 写一个触发器。从tb_control里删除一条记录,自动把该记录添加到另一个表tb_ctrlHistor中 sql读取指定字符前的字符 数据库对象命名参考 【转】 正则表达式入门教程 权限设计 【数据库和代码】 无限级treeview设计 C#编码建议 【程序权限设计】 生意就是这样做成的 SQL Server 存储过程的经典分页
ASP.NET设置ie打印两法
GO-NET · 2007-08-09 · via 博客园 - GO-NET

在ASP.Net中最为头痛的可能就是打印设置了。
为了这个问题,我都要熬白头了。网上也找了很多代码,但是总是会存在一些问题。根据我自己的实际试验发现不能用,为找不到对象之类的错误提示。

这里给出两种实际可用的方法:

一。通过注册表修改IE打印设置
<HTML>
 <HEAD>
  <TITLE>New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="YC">
  <script language="VBScript">
dim hkey_root,hkey_path,hkey_key

hkey_root="HKEY_CURRENT_USER"
hkey_path="\Software\Microsoft\Internet Explorer\PageSetup" //IE打印设置的注册表地址

function pagesetup_null()
on error resume next
Set RegWsh = CreateObject("WScript.Shell")
hkey_key="\header"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"" //页眉
hkey_key="\footer"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"" //页脚
hkey_key="\margin_left"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"1" //键值设定--左边边界
hkey_key="\margin_top"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"1"
hkey_key="\margin_right"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"1"
hkey_key="\margin_bottom"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"1"
end function
'//

function pagesetup_default()
on error resume next
Set RegWsh = CreateObject("WScript.Shell")
hkey_key="\header"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"&w&b页,&p/&P" //页数-
hkey_key="\footer"
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"&u&b&d" //网址,日期等信息
end function
  </script>
 </HEAD>
 <BODY>
  <table width="100%" border=2 cellspacing=1 align=center cellpadding=1 id=tb1>
   <tr>
    <td>
     <br>
     <br>  
     <br>
     <p align="center">
      <input type="button" value="Clean" onclick="pagesetup_null()"> <input type="button" value="Reset" onclick="pagesetup_default()"><br>
     </p>
    </td>
   </tr>
  </table>
 </BODY>
</HTML>

除了以上键值以外还有

duplex
orientation
paper_size
paper_source
printer

注意:上面对页边距的设置为:1,实际设置值为25.4。

昨天发现这个方法有一个致命的缺点。那就是这个只能在安装有.Net框架的机子上有效。因为它所调用的

WScript.Shell.Net框架的...我还以为解决了这个烦人的打印控制了,昨天在客户端一运行简直让我伤心死。呵呵。。。。

难道还要让对什么是.net都不知道的用户去安装.net的框架么。不现实。
所以只能又回到javascript中来了。

二。javascript
今天发现原来以前有时有效有时无效的原因是出在,<input name="idPrint" type="button" value="打印" onclick="doprint()">不能放在页面的<form runat="server" id="Form1">之内,客户端的javascript放在runat server怎么行呢。认识到这个问题就好解决了。以下给出原码:

<script language="Javascript"><!--
function doprint() {

//保留客户打印机设置
  var h = factory.printing.header;
  var f = factory.printing.footer;
  var t = factory.printing.topMargin;
  var b = factory.printing.bottomMargin;
  var l = factory.printing.leftMargin;
  var r = factory.printing.rightMargin;
 
  document.all("printbtn").style.visibility = 'hidden';//打印时隐藏打印按钮
  //设置页眉页脚上下左右边距
  factory.printing.header = "页眉+_+ohiolee的打印世界";
  factory.printing.footer = "想设置页脚么,这里哦";
  factory.printing.portrait=true; //纵 factory.printing.portrait = false; 横
  factory.printing.topMargin="6";//存在最小默认值5.02
  factory.printing.bottomMargin="6";//存在最小默认值4.13
  factory.printing.leftMargin="2";//存在最小默认值5.08
  factory.printing.rightMargin="2";//存在最小默认值6.79。。。本人机子上测出来是这样的,不知道普遍是否如此。
免费版就只能设定这些咚咚了吧
  // 直接打印
  factory.DoPrint(false);//true时弹出打印对话框
  //返回到原来的打印设置
  factory.printing.header = h;
  factory.printing.footer = f;
  factory.printing.topMargin=t;
  factory.printing.bottomMargin=b;
  factory.printing.leftMargin=l;
  factory.printing.rightMargin=r;
  //显示打印按钮
  document.all("printbtn").style.visibility = 'visible';//通过document.all("printbtn").来指定页面中的任何类,并给以进一步属性设置
}
//--></script>
 </HEAD>
 <body>
  <OBJECT id="factory" style="DISPLAY: none" codeBase="ScriptX.cab#Version=5,0,4,185" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
   viewastext>
  </OBJECT>
  <div id="printbtn"><input name="idPrint" type="button" value="打印" onclick="doprint()"></div>codeBase="http://www.meadroid.com/scriptx/ScriptX.cab#Version=5,60,0,360" 这里需要给出ActiveX ScriptX.cab的地址,你可以下载到自己的网站中并提供出来,也可以引用其他网站的。当用户访问该网页时,将自动判断该浏览器是否已装有,没有的话,弹出下载警告。同意下载之后,就可以了。

注意不要把<div id="printbtn"><input name="idPrint" type="button" value="打印" onclick="doprint()"></div>放在runat server的form中哦。。。不要再犯像我一样的错误了哦。

当将打印属性设置函数放在body onload中时,导入该页面时,设置。退出后自动恢复原有设置。

转自 csdn