Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Automate Your Classic ASP Web Forms : Page 4

If you've ever created data-driven forms, especially complex ones, you know how hard it is to make all the control names and database column names match up correctly—and you'll immediately appreciate how the techniques shown in this article can simplify development, reduce errors, and minimize maintenance.


advertisement
Additional Techniques
One HTML form I wrote in the course of a project consists of 167 form fields (checkboxes, radio buttons, text boxes, drop-downs, etc.). The form is divided into several sections. I needed to present the preview information in a way that showed the grouping of the form. For example, one section deals with Attendance, another with Service Needs, another with Agency Participation, etc. In addition, I needed to set up an "alarm" message that would display a "Section Incomplete" message if users failed to select a value within a group. Given the length and complexity of the form, users need to view the various groups of data on their preview page in a printable and compact way. The preview would also need to warn users if they missed an entire section. I wouldn't recommend constructing such a complex form but this was specified in committee!

Previewing Groups of Elements
You can use naming conventions to allow ASP programming logic to find and group the form field contents into sections on the preview page and also use programming logic to determine whether a section has anything selected. For example:

<% ' Initialize counters for checkbox groups on the form AttendanceChecked = 0 InvolvedChecked = 0 BasicNeedsChecked = 0 ...Other Counters As Needed... ' For each group, specify a prefix or suffix in the ' form control name. Use the instr function in ASP to ' identify that a given value exists (a checkbox was ' ticked) For x = 1 To Request.Form.count() Attendance = Instr(Request.form.key(x), _ "_Attendance") Involved = Instr(Request.form.key(x), _ "_Involved") BasicNeeds = Instr(Request.form.key(x), _ "BasicNeed_") ' ...Other Groups as Needed... ' Increment the counter for each ' value that exists AttendanceChecked = AttendanceChecked + _ Attendance InvolvedChecked = InvolvedChecked + Involved BasicNeedsChecked = BasicNeedsChecked + _ BasicNeeds '...Other Counters as Needed... Next ' Build a table to list all the Attendance elements ' if any have been passed to the preview page If AttendanceChecked > 0 then Response.Write "<table border = ""1"" " & _ "width = ""600"">" & _ "<tr><td width=""%100"" colspan = ""3"">" & _ "<p align = ""center""><b>Referring " & _ "Agencies </b></td></tr>" For X = 1 To Request.Form.count() Item = Request.Form.key(x) SectionsChecked = instr(Request.form.key(x), _ "_Attendance") If SectionsChecked > 0 Then ' Use string trim function to display ' just the name of the field and not the ' _Attendance suffix ItemLength = Len(Item) ItemLengthTrim = ItemLength - 11 ItemName = Left(Item, ItemLengthTrim) j = i Mod 3 If j = 0 Then Response.Write "<tr><td>" & _ ItemName & "</td>" Else response.write "<td>" & ItemName & _ "</td>" End If i = i + 1 End If Next Response.Write "</table>" Response.Write "<hr>" Else ' If there were no Attendance fields checked then ' response.write in red a warning that this section ' contains no information Response.Write "<p><b><font color=""#FF0000"">" & _ "No referring agencies specified!" & _ "</font></b></p>" End If %>

Creating an Edit Page
If you submit data to a remote database to which you don't have direct access, you can easily create an ASP page based on the HTML form that can pull up the data for edits. To do that, copy the HTML form, save it as an .ASP file and pull up the needed data from the database using the appropriate SQL SELECT statement. To view the information in the ASP form, simply re-name the form fields by wrapping the name in <%= myRS.Fields("FieldName") %>. Since your HTML form field names match the database fields, this is a relatively trivial operation. After you have made your edits, you can pass the information back to the database using a SQL UPDATE statement.

Some systems may have limits on the number of fields that an UPDATE statement can handle. For example, MS Access will not process an UPDATE with more than 155 fields in the list. You can use the ability to group sets of fields based on standard suffixes or prefixes to build a set of UPDATE statements to work around this limitation.



Steve Skelton is an administrative technician for the Health and Human Services Commission of the State of Texas. You can reach him .
Comment and Contribute

 

 

 

 

 


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

 

 

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