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 2

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
Previewing Form Data In Table Format
The following code shows how to extract a list of form field elements and values and view them in a table for the preview page. This technique is extremely useful for debugging.

<% I = 0 Response.Write "&lt;table border = ""1"" & _ width = ""500""&gt;" & _ "&lt;tr&gt;&lt;td width=""%100"" colspan = ""3""&gt;" & _ "&lt;p align = ""center""&gt;" & _ "&lt;b&gt;Table Title&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;" For x = 1 To Request.Form.count() item = Request.Form.key(x) Select Case item Case "Control_Name1_Do_Not_List" Case "Control_Name2_Do_Not_List" Case " Control_Name3_Do_Not_List" Case "B1" 'Submit Button Name Case Else j = i mod 3 If j = 0 Then response.write "&lt;tr&gt;&lt;td&gt;" & Item & "&lt;/td&gt;" Else response.write "&lt;td&gt;" & Item & "&lt;/td&gt;" End If i = i + 1 End Select Next %>

There are some considerations to keep in mind. The loop in the preceding code finds every named control on the form page; however, not every control contains a data value destined for the database. At minimum, you'll need to skip the Submit Button name/value. I use the Select Case construct for skipping values I don't want displayed because it is easier to read and edit than If...Then logic.

Gathering Hidden Field Values
As you might expect, gathering the data for the submit operation is even more straightforward than constructing a table to preview the form data. When the user submits the main form page, you use a loop similar to the one you've already seen to retrieve the form values and output a list of hidden field tags that you'll use later when the user approves the data and submits the preview form.

<% MonthlyDate = Request.Form("Date_month") & "-" & _ Request.Form("Date_day") & "-" & _ Request.Form("Date_year") For Each item In Request.Form Select Case item Case "Date_month" Case "Date_day" Case "Date_year" Case "B1" Case Else ' create a hidden field containing the data value quote=chr(34) Response.Write "&lt;input type=""hidden""" & _ "name=" & quote & item & quote & " value=" & _ quote & Request.Form(item) & quote & "&gt;" End Select Next Response.Write "&lt;input type=""hidden""" & _ name=""MonthlyDate"" value=" & quote & _ MonthlyDate & quote &"&gt;" %>

The preceding code skips some form names and values—the Submit button and three other fields used to create the value for the MonthlyDate variable. The MonthlyDate value requires special formatting to ensure the entire value gets written to the hidden form field. In this case, the last line wraps the MonthlyDate value in quotes before creating the hidden input tag. Again, using the Select Case construct makes it easy to create and maintain the logic to skip special and/or unwanted field names and values.

Author Note: field values with spaces may get truncated in the hidden field list if the value is not wrapped in quotes. The code above should do this automatically. In addition, form values with apostrophes may cause the submit operation to fail. You can catch this possibility when building the hidden fields by using the Replace operation in the code:


response.write "<input type=""hidden"" name=" & quote & item & quote & " value=" & _ quote & Replace(Request.Form(item), "'", "''") & quote &">"





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