Asp. NET Set IE print Two method __.net

Source: Internet
Author: User
Tags visibility
Asp. NET Set IE Print Two method < turn > The most headache in asp.net is the printing setup.
I'm going to get through this for a while. A lot of code has been found on the internet, but there are always some problems. According to my own actual experiment found not to use, in order to find no object such as Error tips.

Here are two methods that are actually available:

One. Modify IE print settings through the registry
<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 the registry address for print settings

function Pagesetup_null ()
On Error Resume Next
Set regwsh = CreateObject ("Wscript.Shell")
hkey_key= "/header"
Regwsh.regwrite Hkey_root+hkey_path+hkey_key, ""//Header
hkey_key= "/footer"
Regwsh.regwrite Hkey_root+hkey_path+hkey_key, ""/Footer
hkey_key= "/margin_left"
Regwsh.regwrite Hkey_root+hkey_path+hkey_key, "1"//Key value setting--left border
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 page, &p/&p"//Page-
hkey_key= "/footer"
Regwsh.regwrite Hkey_root+hkey_path+hkey_key, "&u&b&d"/url, date and other information
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>

In addition to the above key values, there are
Duplex
Orientation
Paper_size
Paper_source
Printer

Note: The page margin is set to: 1, and the actual setting is 25.4.

The method was found to have a fatal disadvantage yesterday. That's the only thing that can be installed there. NET Framework is available on the machine. Because the Wscript.Shell it calls is the. NET Framework ... I thought I had solved this annoying print control, and yesterday at the client-run almost made me sad to die. Oh....

Do you want to install the. NET Framework for users who don't know anything about. NET? Not realistic.
So I can only go back to JavaScript again.

Two. Javascript
Today found that the original sometimes effective sometimes invalid reason is out, <input name= "Idprint" type= "button" value= "print" onclick= "Doprint" () "> cannot be placed on the page <form runat= "Server" id= "Form1" >, the client's JavaScript is placed in runat server how to do it. It is a good solution to realize this problem. The following gives the original code:

<script language= "Javascript" ><!--
function Doprint () {
Keep Client printer settings
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 ';//Hide Print button when printing
Set header and footer up and down margins
Factory.printing.header = "Print world with header +_+ohiolee";
Factory.printing.footer = "Want to set footer, here Oh";
factory.printing.topmargin= "6";//There is a minimum default value of 5.02
factory.printing.bottommargin= "6";//There is a minimum default value of 4.13
Factory.printing.leftmargin= "2";//There is a minimum default value of 5.08
Factory.printing.rightmargin= "2";//There is a minimum default value of 6.79 ... I measured out on the machine is like this, I do not know whether this is generally the case.
Print directly
Factory. Doprint (false); Pop-up Print dialog when//true
Go back to the original print settings
Factory.printing.header = h;
Factory.printing.footer = f;
factory.printing.topmargin=t;
Factory.printing.bottommargin=b;
Factory.printing.leftmargin=l;
Factory.printing.rightmargin=r;
Show Print Buttons
document.all ("Printbtn"). style.visibility = ' visible ';//Through document.all ("printbtn"). To specify any class on the page and give further property settings
}
--></script>
</HEAD>
<body>
<object id= "Factory" style= "Display:none" codebase= "" scriptx.cab#version=5,0,4,185 "CLSID" : 1663ed61-23eb-11d2-b92f-008048fdd814 "
Viewastext>
</OBJECT>
<div id= "printbtn" ><input name= "Idprint" type= "button" value= "print" onclick= "Doprint ()" ></div>

codebase=http://www.meadroid.com/scriptx/scriptx.cab#version=5,60,0,360 Here you need to give an ActiveX Scriptx.cab's address, you can download it to your website and provide it, or you can refer to other websites. When the user accesses the Web page, it automatically determines whether the browser is installed and, if not, pops up the download warning. After agreeing to download, it's OK.

Be careful not to <div id= "printbtn" ><input name= "Idprint" type= "button" value= "print" onclick= "Doprint ()" ></div> Put it in the form of runat server ... Don't make a mistake like me again.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.