asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Advanced Form Presentation and Printing w/ PDF, FDF, ASP, and DHTML - Part 3
By Eric Coffman
Rating: 2.8 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Let's say that you can't guarantee that your clients or users have Adobe Acrobat Reader installed or you don't want to spend $250 - $300 for the full version of Adobe Acrobat. However, you want to present a pleasant-looking form (perhaps matching you site's color scheme or style) that can be printed out without all those silly-looking text-box borders and drop-down arrows, etc. Sure, you can get rid of those text-box borders with a style sheet, but they still have a definite width. What if your client has a last name so long that when the form is printed, part of the last name is cut off? Or, just the opposite occurs, your client has a 2 or 3 letter last name, and you have just wasted an incredible amount of space trying to accommodate a client with a long last name. And what about those drop-down arrows or the other form field types? This article will show you how to separate your form presentation in a browser from the way the form prints out without processing the form through a CGI script or other means. Everything is handled client-side. The user can print out the form even before it is submitted.

    Download the completed source here: 010824.zip

    Read Part I and Part II of this article.

    Getting Started

    We will discuss the handling of the most common form elements in detail. I will not discuss client-side or server-side validation. Everything discussed here will be relative to the form itself.

  • The text box and text area will be handled identically.
  • The check box and radio button will be handled identically.
  • The drop-down and list box will be handled identically without the support of multiple selections.
  • The Submit button will be handled normally with alternatives using the little-known button tag.

    Everything discussed is HTML 4.01, CSS Level 2, and JavaScript 1.2 compliant. All code examples have been tested on an IIS 4 server with an IE 5.1 browser on the client.

    Required Software

    None! (Except, perhaps, a Microsoft Internet Explorer 5.0 browser or above.)

    Step 1: Create a basic HTML form using all of the above-mentioned form elements. Immediately after each form element (with the exception of the check box and the radio buttons) write an empty opening and closing font tag. All of the form elements (minus the exceptions) will have a class named "display" and all the empty font tags will have a class named "print." Add three buttons --, a Submit, Print, and a Reset button. The buttons will also have a class named "display." This example is not fancy, but you may get as fancy as you want.

    
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Form Display & Print Test</title>
    </head>
    
    <body>
    
    <form name="frmTest">
    <table border="0" cellpadding="5" cellspacing="5" width="100%">
      <tr>
        <td width="33%">Enter your first name:</td>
        <td width="33%">Describe your programming experience:</td>
        <td width="34%">Choose the language(s) you are familiar with:</td>
      </tr>
      <tr>
        <td width="33%" valign="top">
          <input type="text" name="txtName" size="20" class="display">
          <font class="print"></font>
        </td>
        <td width="33%">
          <textarea rows="4" name="txtExperience" cols="29" class="display"></textarea>
          <font class="print"></font>
        </td>
        <td width="34%">
          <input type="checkbox" name="chkVBS" value="ON"> VBScript<br>
          <input type="checkbox" name="chkC" value="ON"> C++<br>
          <input type="checkbox" name="chkJava" value="ON"> Java
      	</td>
      </tr>
      <tr>
        <td width="33%">Pick your gender:</td>
        <td width="33%">Select your favorite site:</td>
        <td width="34%">Select the most appealing HTML color:</td>
      </tr>
      <tr>
        <td width="33%">
          <input type="radio" value="Female" name="radGender"> Female<br>
          <input type="radio" name="radGender" value="Male"> Male
      	</td>
        <td width="33%">
          <select size="3" class="display" name="selSite">
            <option>internet.com</option>
        	<option>microsoft.com</option>
        	<option>netscape.com</option>
        	<option>sun.com</option>
        	<option>yahoo.com</option>
          </select>
          <font class="print"></font>
        </td>
        <td width="34%" valign="top">
          <select size="1" name="selColor" class="display">
        	<option>#FFFF00</option>
        	<option>#000080</option>
        	<option>#DF0029</option>
        	<option>#666666</option>
        	<option>#009F62</option>
          </select>
          <font class="print"></font>
       	</td>
      </tr>
      <tr>
        <td width="33%" align="right">
          <input type="submit" value="Submit" name="btnSubmit" class="display">
        </td>
        <td width="33%" align="center">
          <input type="button" value="Print" onClick="print();" name="btnPrint" class="display">
        </td>
        <td width="34%">
          <input type="reset" value="Reset" name="btnReset" class="display">
        </td>
      </tr>
    </table>
    </form>
    
    </body>
    
    </html>
    
    
    Step 2: Add the following JavaScript functions into your header. Add an onBlur attribute to each of your text box and text area elements. Add an onChange attribute to each of your select elements.
    
    onBlur="loadTxtPrint(this);" and onChange="loadSelPrint(this);" 
    
    
    After you save your file, you may test out your form to make sure the JavaScript functions are working.
    
    <script language="JavaScript">
    <!--Hide from legacy browsers
    
    function loadTxtPrint(box){	 
    	//Text box or text area onBlur function. Stores the DOM index
    	
    	var i = box.sourceIndex + 1;	
    		// -of the calling Select box and adds 1 to that index.
    	
    	var sValue = box.value;		
    		//Stores the text of a text box or text area.
    	
    	var oHidBox = document.all[i];	
    		//Stores a ref. to the next DOM index (our empty font tag).
    	
    	oHidBox.innerHTML = sValue;	
    		//Writes the text into the empty font tag.
    }
    	
    function loadSelPrint(box){	
    	//Select box onChange function. Stores the DOM index of the calling 
    	
    	var i = box.sourceIndex + box.length + 1;	
    		//-select box and adds the number of options plus 1 to that index.
    	
    	var sel = box.selectedIndex;	
    		//Stores the index of the selected item of the calling Select box.
    	
    	var sValue = box.options[sel].text;	
    		//Stores the text of the selected item using the Select box index. 
    	
    	var oHidBox = document.all[i];	
    		//Stores a reference to the next DOM index (our empty font tag).
    	
    	oHidBox.innerHTML = sValue;	
    		//Writes the Select box's selected text into the empty font tag.
    }
    
    //-->
    </script>
    
    
    Step 3: Add the following style sheet into your header.
    
    <style type="text/css">
    <!-- Hide from legacy browsers
    
    /* Specifies how tags with these classes are displayed in a browser */
    @media screen {
    
    /* All the form elements (except radio & check boxes) have this class */	
    .display { 
      font-family: Verdana; 
      font-size: 8pt; 
    }
    
    /* The empty font tags will not be displayed */
    .print { 
      display: none;
    }
    
    }
    
    /* Specifies how tags with these classes are printed */
    @media print {
    
    /* All the form elements (except radio & check boxes) will not be printed */
    .display {
      display: none;
    }
    
    /* But our 'empty' font tags will be printed */
    .print {
      font-family: "Times New Roman"; 
      font-size: 10pt;  
    }
    
    }
    
    //-->
    </style>
    
    

    Conclusion

    What we have done is dynamically filled the inner HTML of our initially empty font tags and set their display style to none. This process is basically reversed using the @media types in a style sheet. By combining all three elements of what is commonly referred to as DHTML, we have produced a powerful, but simple way of using HTML forms. The downside is that the client or user must have scripting enabled for this to work, whereas the use of Adobe Acrobat does not. Also Adobe Acrobat includes its own form validations and supports custom JavaScript scriptlets that I believe are independent of the browser's settings.

    As promised, I will briefly mention the button tag. The button tag is unique in that it has an opening and closing tag (<button various attributes>Font and/or Image tags</button>) and you can put font tags and/or image tags within the enclosed button tag. You can set the Type attribute just as you would in an input tag. What this allows is a combination of text and an image on a button. You can even perform image rollovers on the button!

    Resources

    For an article on "Using Stylesheets with IE 5," see http://hotwired.lycos.com/webmonkey/99/37/index1a.html.

    For information on media types, see http://www.w3.org/TR/REC-CSS2/media.html.

    For information on the BUTTON element, see http://www.w3.org/TR/html4/interact/forms.html#h-17.5.

    About the Author

    Eric Coffman is currently working as an ASP (mostly!) programmer for SIMA San Diego's Web Development and Information Systems. Adobe Acrobat PDF forms are used to automatically route various requests through a user's specific chain of command within an intranet environment. He can be contacted at komodo@backpacker.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    Proposion N2N
    Proposion N2N connects Microsoft .NET applications to Lotus Notes and Lotus Domino databases. This ADO.NET managed data provider allows you to perform blindingly fast queries and updates of Notes data from ASP.NET pages, .NET web services, Windows, or Mobile applications. An innovative SQL-like query language leverages the unique features of Notes and makes collaborative software accessible to relational database programmers.
    [Top]
    Other Articles
    Sep 15, 2005 - Building an Image Keyword System
    Unlike text-based file formats image files aren't made up of words, which makes searching for an image file by keyword difficult. Instead of being able to simply open the file to see what it contains, we're stuck looking at the text around it and other metadata to determine the image's meaning. In this article, Ziran Sun shows you how to build a simple database-based image keyword system that allows you to associate keywords with images and use these keywords to make finding images easier.
    [Read This Article]  [Top]
    Apr 7, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
    In the second part of of his article on using MySQL with ASP.NET, Ziran Sun covers how to add a new MySQL user to the database server, assign the user the appropriate permissions, connect to the database, and build a simple ASP.NET page to perform a query.
    [Read This Article]  [Top]
    Feb 10, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 1
    Back in the days of classic ASP, if you were building a database-driven web site, your choice was either to invest a lot of money to get a copy of Microsoft SQL Server (or some other enterprise-ready database) or invest a lot of time finding a way to deal with the performance and scalability limitations of Microsoft Access. Luckily these days there's another viable alternative: MySQL.
    [Read This Article]  [Top]
    Jan 27, 2005 - Moving a Database from SQL Server 7.0 to SQL Server 2000
    Moving or copying a SQL Server database from one machine to another requires a lot of preparation in order to ensure a smooth transfer. In this article, Dina Fleet Berry examines the different methods and highlights the different issues associated with each of them.
    [Read This Article]  [Top]
    Jan 6, 2005 - Debugging a SQL Stored Procedure from inside SQL Server 2000 Query Analyzer
    There are many times when using SQL Server 2000 Query Analyzer to debug SQL statements is a better choice than debugging in Visual Studio .NET. In this article, Dina Fleet Berry explains why and walks you through the debugging process step-by step.
    [Read This Article]  [Top]
    Nov 24, 2004 - Persisting .NET Objects to SQL Server Using SQLXML and Serialization
    As a follow up to his article on retrieving objects from SQL Server using SQLXML and serialization, Gianluca Nuzzo discusses saving objects back to SQL Server using a schema definition file and updategrams.
    [Read This Article]  [Top]
    Sep 14, 2004 - Transaction Processing in ADO.NET 2.0
    One area that stands out when comparing ADO.NET 1.x to ADO.NET 2.0 is transaction processing. Bill Ryan shows just how easy transaction processing has become with the TransactionScope object in ADO.NET 2.0.
    [Read This Article]  [Top]
    Sep 8, 2004 - Custom Object Data Binding with .NET
    Developers often use brute force coding to marshal data between the GUI and application objects. In this article, Luther Stanton explains how to use .NET's out-of-the box data-binding functionality to make this job much easier.
    [Read This Article]  [Top]
    Sep 2, 2004 - Queue MSMQ Messages from SQL Server
    Learn how to create a console application to queue a message in Microsoft Message Queuing (MSMQ) and then use an extended stored procedure to call the console application from a SQL Server trigger.
    [Read This Article]  [Top]
    Aug 30, 2004 - Tuning Up ADO.NET Connection Pooling in ASP.NET Applications
    Connection pooling increases the performance of Web applications by reusing active database connections instead of creating a new connection with every request. This article shows how to monitor the connection pool, diagnose a potential problem, and apply the appropriate fix.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers