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


Form-Building Routines III—the Conclusion  : Page 5

Use automatic control-building routines to create and populate checkbox and radio button controls from a database.


Data Bound Radio Buttons
Just as with the combo and listbox controls in the previous article, you can build routines that obtain the values for radio buttons. All you need to do is provide the appropriate database table and field names to the routines.

For example, if you are building a web page that presents a survey or a questionnaire and the answers for a given question are stored as records in a table, you can use the following "DisplayRadioButtonsFromDB" routine to present all the answers as radio buttons.. The routine (see Listing 1) accepts the 7 arguments shown in Table 2:

Table 2: Arguments for the DisplayRadioButtonsFromDB routine.

StrLabel The text for the label
strFieldName The name of the radio button group
strValue The index of the radio button that should be selected by default.
StrLookupTable The database table to use to find all available values for this set of radio buttons.
StrLookupID The primary key, unique id within the table, this will be assigned to the VALUE property of the radio buttons
StrLookupDesc The text of the radio button to display
blnRequired A Boolean flag to indicate this group of radio button is a required control on the form

Listing 1 contains the complete code for the DisplayRadioButtonsFromDB routine. You begin by outputting the label for the group of radio buttons:

   ' -- Output the Label
   Response.write "<TD ALIGN=""RIGHT"" VALIGN=""TOP"">" 
   If blnRequired Then
      Response.write "<FONT COLOR=""BLUE"">"
   End if
   Response.Write strLabel & "&nbsp;</FONT></TD>"
   ' -- Now the table cell containing all our radio buttons
   Response.Write "<TD ALIGN=""LEFT"" VALIGN=""TOP"">"
The code caches the HTML for the radio buttons in an Application variable the first time it runs. Therefore, you should check to see if the output is already cached. . If it is, then you can reuse it.

      ' -- Check the CACHE first
      strResult = Application("opt" & strFieldName)
      If strResult = "" Then
If the output is not cached, you must access the database to obtain the data. There are two possible techniques: obtain the data from the database and then generate HTML text from it, or have the database return records containing pre-built HTML.

      ' -- build your SQL statement
      strSQL = "SELECT " & strLookUpID & "," & _
         strLookUpDesc & " FROM " & strLookUpTable
      ' -- order by clause if needed
      strSQL = strSQL & " ORDER BY " & strLookUpDesc
      ' -- use the SQL Statement with your standard data 
      ' -- access method to obtain a recordset
      '   ' -- build your SQL statement
      '   strSQL = "SELECT '<INPUT TYPE=""radio""" & _
      NAME=""" & strFieldName & """ VALUE=""'" & _
      "+ CONVERT(VARCHAR(5), " & strLookUpID & _
      ") + '"">' + RTRIM(" & strLookUpDesc & _
      ") + '<BR>' AS TheField FROM " & _
      '   ' -- order by clause if needed
      '   strSQL = strSQL & " ORDER BY " & strLookUpDesc
Retrieve the data in a recordset (code omitted) using whatever standard technique you favor. If you're using the first technique, iterate through the recordset building the HTML string:

      ' -- assuming the variable objRS contains a 
      ' -- valid recordset   
      If (objRS.BOF and objRSC.EOF) Then
         strResult = ""   ' No records found
         strResult = ""
         Do While Not objRS.EOF
            strResult = strResult & _
            "<INPUT TYPE=""radio"" NAME=""" & _
               strFieldName & """ " 
            strResult = strResult & "VALUE=""" & _
               objRS(strLookUpID) & """>"
            strResult = strResult & _
               objRS(strLookUpDesc) & "<BR>"
      End if    ' if (objRSC.BOF and objRSC.EOF) Then
Notice that the code uses the same name for all radio buttons (making them part of a single group) and assigns the unique ID field (strLookUpID) to the VALUE attribute and uses the description field (strLookUpDesc) for the text of the radio buttons. End each line with a HTML break character (<BR>) so the radio buttons appear in a vertical list.

If you use the second technique, the code is shorter because the database builds the HTML text for you.

      If (objRS.BOF and objRS.EOF) Then
         strResult = ""   ' No records found
         Do While Not objRS.EOF
            strResult = strResult & objRS("theField")
      End if    ' if (objRSC.BOF and objRSC.EOF) Then
Finally, cache the generated HTML string in an application level variable so you don't have to hit the database for every request.

   ' -- strResult. Place it in the Application Cache 
   ' -- for next go around
         Application("opt" & strFieldName) = strResult
The routine step pre-selects one of the radio buttons based on the value of the "strValue" argument sent to the routine:

      ' -- Handle the Pre-Selection of the RADIO 
      ' -- buttons control
      ' -- All we need to do is to add the word CHECKED
      ' -- next to the item whose value = strValue
      strResult = Replace(strResult, "VALUE=""" & _
         strValue & """","VALUE=""" & strValue & _
         """ CHECKED")
Finally, output the result:

      ' -- Output the list of radio butotn tags
      Response.write strResult
      Response.Write "</FONT></TD></TR>"
Building data bound check boxes uses an identical technique, so I'll won't cover it in this article.

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