print web page

yfng

Member
Joined
Feb 23, 2005
Messages
13
Programming Experience
Beginner
I am doing a web project.
Is there any method to print web pages with support of page breaks and without printer prompt diaglog e.g.like window.print() (or close immediately) by just clicking a button on the webpage?

[ammend] For page breaks, I know
HTML:
<div></div>
can be used. How about close the pop-up print dialog and print the web pages (or directly print out web pages)?

Thanks.
 
Last edited:
Short answer is that it is not possible. If you could design a webpage that caused something to print with basically no user interaction you would have some very upset web surfers....

Now long answer is that if you know that your users are using IE it can be done something like this.

Inside a button event:
VB.NET:
 Dim Script As String
		Script = "<script language=""JavaScript"">" & vbCrLf & vbTab & "var NS = (navigator.appName == ""Netscape"");var VERSION = parseInt(navigator.appVersion);if (VERSION > 3) {prIntthis();}" & "<" & "/script>"
 		lblPrInt.Text = Script
 		lblPrInt.Visible = True

Now in my HTML I have the following:
VB.NET:
 <HEAD>
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/nav4-0">
 		<script language="JAVASCRIPT">
 		 function move() {
				window.location = '/default.aspx'
 			}
 	
 			function printthis(){ 
 				if (NS) {
					window.print() ; 
 				} else {
		 		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		 	 document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		 		WebBrowser1.ExecWB(6, -1);//Use a 1 vs. a 2 for a prompting dialog box 
					WebBrowser1.outerHTML = ""; 
					timer=setTimeout('move()', 3000);
 				}
 			}
 		</script>
 	</HEAD>

As for forcing page breaks I kn ow that it can be done, but have not done it myself....
 
Thank you for your reply.

The requirement is like that:
User selects some rows in the datagrid and there is a button in the web for printing the selected rows. However, the print action needs to update database and then prints out selected rows.

Will there be any better methods? Thanks.
 
In order to do that, you would have to code the button event to update the database and format the page exactly the way you want it to print. Once you have that done you add the following at teh end of the button_click event code:
VB.NET:
Dim Script As String
Script = "<script language=""JavaScript"">" & vbCrLf & vbTab & "var NS = (navigator.appName == ""Netscape"");var VERSION = parseInt(navigator.appVersion);if (VERSION > 3) {prIntthis();}" & "<" & "/script>"
lblPrInt.Text = Script
lblPrInt.Visible = True

You only need an invisible control called lblPrint on the form, and the other HTML code I listed before.
 
Back
Top