Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Build Printable ASP.NET Pages
Provide users with a variety of printing capabilities in your ASP.NET Web applications.
by Doug Thews

August 13, 2004

Technology Toolbox: C#, ASP.NET, Visual Studio .NET 2003, JavaScript

Users should be able to print order confirmations, invoices, and the like directly from your Web application. And no, the old Shift + Print Screen approach from the 3270 mainframe emulation screen days won't cut it, for the same reason that the JavaScript window.print() command often won't, even though it triggers the operating system's Print dialog with minimal code:

<A href="javascript:window.print()">
   Print Me!</A> 

This gives users that invoice they want—plus a lot more that they don't, including application navigation controls, superfluous graphics, command buttons, and Web site headers/footers. Also, JavaScript is vulnerable to hacks and viruses, so many users turn off script support in their browsers. This nullifies even the most elegant JavaScript solution unless you can be sure the target audience has it enabled.

ADVERTISEMENT

The safest way to provide effective printing capabilities is to render a "printable page" that users can print using their browser's Print command. You pre-format a printable page to contain only user-relevant information, the code renders the page, users initiate the browser's Print command, and the operating system brings up its Print dialog. You can automate this process in JavaScript if your users' browsers support that.

You can also use frames. Many Web sites provide specific areas for menu/navigation, site headers, and content using frames, though this method seems to be fading compared to using DIV and IFRAME areas. Still, you might be able to use a frame as a printable page if you've set up your Web app to use frames and you put all your result content into a frame. Have users print the Content frame from within the browser by right-clicking within the frame and selecting Print. This works if you're using current versions of IE or Netscape (again, you could automate this process in JavaScript).

Suppose you have a page with a frameset containing two frames, "Menu" and "Content":

<frameset cols="20%,*" border="0">
   <frame name="Menu" 
      src="OrderMenu.aspx">
   <frame name="Content" 
      src="SimplePrintingWithFrames.aspx">
</frameset>

Add the code for printing the Content frame from a button:

<INPUT type="button" value="Print Content" 
   OnClick="javascript:PrintContent()">
<script language="JavaScript"><!--
function PrintContent() {
   parent.Content.focus();  
   // Required to support IE
   parent.Content.print();
}
//--></script>



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home