Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Save Time with Form-Building Routines : Page 3

Build HTML forms in no time with ASP.


The Code Explained
The first piece of code we call is the routine WriteFormBegin. This takes a few arguments and creates the HTML FORM tag:

<FORM NAME="frmInfo" ID="frmInfo ACTION="pgApply.asp" METHOD="POST"
 ONSUBMIT="return ValidateForm(this);">

And this is the routine that produced it:

Sub WriteFormBegin(byval strFormName, byval strFormAction, _
byval strFormMethod, byval strFormSubmit)
Response.Write "<FORM "
	If strFormName <> "" Then
		Response.Write " NAME=""" & strFormName & _
""" ID=""" & strFormName & """"
	End if
	Response.Write " ACTION=""" & strFormAction & """"
	Response.Write " METHOD=""" & strFormMethod & """"
	If strFormSubmit <> "" Then
		Response.Write " ONSUBMIT=""" & strFormSubmit & """"
	End if
	Response.Write ">"
End Sub

The routine creates the form begin tag based on the arguments sent to it. Two of the arguments are really optional, however, in VBScript, since there is no optional argument type, we check to see if the first and the last arguments are blank. If they are, no action is taken, otherwise, the HTML acquires a NAME (and ID) and an ONSUBMIT attribute. Notice that all values are enclosed within double quotes. To make sure you have a double quote (") character within an ASP string, you use double double quotes ("") to indicate a literal double quote.

Note: I am writing this code on a Windows 2000 IIS server. In that scenario, the ASP Response.Buffer property is true by default. Hence the multiple Response.write statements do not pose performance degradation. Because Response buffering is true, all the response.writes are batched together and sent to the browser as one. If you are using ASP under Windows NT, you might want to set the Response.Buffer to true at the top of the page.

The last call we make is to close the form and it is very simple: WriteFormEnd. All it does is write out the "</FORM>" tag.

Sub WriteFormEnd()
	Response.write "</FORM>"
End Sub

The next two calls create and end an HTML table:

Sub WriteTableBegin(byval intTableSize)
	Response.Write "<TABLE BORDER=""0"" CELLPADDING=""2"" CELLSPACING=""0"""
	If intTableSize <> "" Then
		Response.Write " WIDTH=""" & intTableSize & """"
	End if
	Response.Write ">"
End Sub
Sub WriteTableEnd()
	Response.Write "</TABLE>"
End Sub

The DisplayTextField Routine
We now come to the heart of this column, the DisplayTextField routine. This is the actual routine that displays a label and a text box combination in two columns of a table. It accepts six arguments:

Argument Description
strLabel The text for the label to use
strFieldName The name of the text box
strValue The value inside the text box
intSize The size of the text box
intMaxLength The maximum length of the data allowed inside the text box
blnRequired Flag to indicate this is a required field

In our example code above, we are using the contents of a recordset object ("objRS") to populate the values of the text box.

This is the code within the DisplayTextField routine:

Sub DisplayTextField(byval strLabel, byval strFieldName,  _
	byval strValue, byval intSize, byval intMaxLength, _
	byval blnRequired)
	Response.Write "<TR>"
	Response.write "<TD ALIGN=""RIGHT"" VALIGN=""TOP"">" 
	If blnRequired Then
		Response.write "<FONT COLOR=""BLUE"">"
	End if
	Response.Write strLabel & " </FONT></TD>"

	Response.Write "<TD ALIGN=""LEFT"" VALIGN=""TOP"">"

	Response.Write "<INPUT TYPE=""TEXT"" " & _
	" NAME=""" & strFieldName & """ " & _
	" VALUE=""" & strValue & """ " & _
	" SIZE=""" & intSize & """ " & _
	" MAXLENGTH=""" & intMaxLength & """>"
	Response.Write "</TD>"
	Response.Write "</TR>"	
End Sub

The DisplayTextField outputs a label and a text box using this HTML text:

TYPE="TEXT" NAME="first_name" 
	SIZE="60" MAXLENGTH="35" VALUE=""></TD></TR>

In this case, I used the design in which a required field's label is indicated in blue, while an optional field's label will be displayed in black (the default). You can modify this by using a scheme where a required field will be in red or bold, or use an additional IMAGE (an asterisk or a blurb) next to the label or text box.

By using the above routine, the job of displaying 10 separate labels and text boxes with different values, options, sizes, and appearances has been reduced to 10 lines of ASP code in the calling program. If you place these routines within a common "include" file, you can reuse this code over and over again in several different pages.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date