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
 

Form-Building Routines III—the Conclusion

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


advertisement
f you've been following this series, the last couple of articles (see Resources) focused on writing some useful "form-building" routines in ASP pages. I'll continue this month by developing routines for displaying check boxes and radio button controls. Again, like last month's article, the focus will be on building data-bound controls.

The previous articles showed:

  • a manual way to build a form containing labels and text boxes, select controls (combo and list boxes)
  • how to automate the process by building functions that build the HTML for labels and controls
  • how to automate building data-bound controls
  • how to handle form field validation
Building Check boxes and Radio buttons.
Building check boxes and radio buttons in HTML is simple. To generate a checkbox, you use the INPUT tag with a TYPE attribute of CHECKBOX.



<INPUT TYPE="checkbox" NAME="chkName" VALUE="1">


The preceding code line generates only the checkbox, the little control that you can check "on" or "off." Place the text for the check box label after the <input> tag:

<INPUT TYPE="checkbox" NAME="chkName" VALUE="1"> Please inform me via e-mail.


The VALUE attribute determines the value the form sends to the server when the user selects the checkbox. By default, checkboxes return on when they have no explicit VALUE attribute; however, using a value of "1" makes it easier to determine if the user checks the control. If the user doesn't check the control, the form returns no value, regardless of the VALUE attribute.

Displaying a radio button is very similar. The following code displays two radio buttons that belong to the same group—that is, only one of the radio buttons in the group can be selected. You create a group by naming a set of radio buttons with the exact same name.



<INPUT TYPE="radio" NAME="optName" VALUE="1"> Male <INPUT TYPE="radio" NAME="optName" VALUE="2"> Female


Figure 1. Simple HTML Form: Use VALUE properties to determine checked buttons.
For a group of radio buttons, you use the VALUE property to determine which radio button was selected. Radio buttons also return "on" by default, but it's bad design to have just one radio button on a form; therefore you should always set the VALUE property for radio buttons.

Consider the following scenario. You're building a form that looks like Figure 1.

This simple form contains a group of radio buttons and one checkbox. In a more complex situation you might set the radio button and check box captions and values from values stored in a database.


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