ASP.NET provides a variety of validation Web controls that can be used to validate a user's form field inputs. Unfortunately, the validation Web controls do not work with the CheckBox or CheckBoxList Web controls. If you set a validation control's
ControlToValidate property to the
ID of a CheckBox or CheckBoxList, the page will throw an
HttpException, stating: "Control 'controlID' referenced by the ControlToValidate property of 'validationControlID' cannot be validated."
There may be times, however, when you need to provide validation for a CheckBox or CheckBoxList. In fact, a rather popular article on 4Guys is Creating a Validation Control for CheckBoxLists by Cenk Civici and yours truly, which shows how to create a CheckBoxList validation control to ensure that at least one checkbox from the list is selected. Similarly, I recently got an email from a reader of my Working with Data in ASP.NET 2.0 tutorial series asking about validating a CheckBox being checked. For example, many Web pages with Terms of Service include a CheckBox titled "I agree to the above terms" that must be checked before continuing.
To provide such validation, we have three choices:
- Forgo any sort of validation Web control semantics and perform the validation check using code on postback. The downside of this is that it breaks from the standard validation control metaphor and requires extra effort to include client-side validation.
- Use the CustomValidator control and define our own server-side and client-side validation logic. The benefit of this approach is that it adheres to the validation control metaphor; however, the validation logic is tightly bound to the ASP.NET page, meaning that the server-side and client-side validation must be replicated on all pages that need to validate a CheckBox or CheckBoxList.
- Create a custom, compiled validation server control that provides the functionality needed. The benefit of this approach is that we have a reusable, easily deployable custom server control that adheres to the validation control metaphor. Unfortunately, this option requires the most upfront code/effort.
Yesterday, I decided to write a custom, compiled validation control for the CheckBox and one for the CheckBoxList using the .NET Framework 2.0 (thereby targetting ASP.NET 2.0). Unlike the CheckBoxListRequiredFieldValidator created by Cenk, these two validators also include client-side script validation support. You can download the CheckBoxValidator and CheckBoxListValidator controls from My Code Projects; a discussion on the CheckBoxList control can be found in the latest 4Guys article, Creating Validator Controls for the CheckBox and CheckBoxList.
This Saturday - September 23rd - I'll be presenting my talk, Top 10 ASP.NET Tips and Traps, at the SoCal .NET Technical Summit in Irvine, California. It's not too late to sign up! For $99 you get a full day of sessions from a variety of top-notch speakers (including Rocky Lhotka, Scott Stanfield, Bill Vaughn, Chris Rolon, and others), organized into four tracks: Web/Atlas, Data, Architecture and Enterprise Practices, and Vista/WPF/WCF/WWF.
Earlier I listed the tips and traps I was planning on presenting, and asked for any feedback. I ended up settling on the following list (in the following order), organizing the tips/traps into four categories:
- TIP: Use SQL Cache Dependencies
- TIP: Use HttpContext.Items as a Per-Request Cache
- TRAP: Avoid a Common Caching Race Condition
- TIP: Improve Paging Performance with Custom Paging
- TIP: Disable View State (Where Appropriate) to Reduce Page Bloat
- TIP: Know the Client-Side Enhancements in ASP.NET 2.0
- TRAP: Beware Broken Links and Images in Master Pages
- TIP: Master Visual Studio 2005's Enhancements
- TIP: Use Reflector to Explore Framework Code
- TRAP: When Using Membership, Don't Forget to Set applicationName
You can download this presentation at http://datawebcontrols.com/classes/TipsAndTraps.zip; to see it presented in person, head on down to the SoCal .NET Technical Summit! Hope to see you there!
My Working with Data in ASP.NET 2.0 tutorial series has been updated to include five new tutorials: one on working with custom Button actions and four on displaying data with the DataList and Repeater controls!
The four tutorials on the DataList and Repeater begin the second phase of the tutorial series, turning attention from the GridView, DetailsView, and FormView controls to the DataList and Repeater controls. Over the course of the four new ones and 14 remaining tutorials yet to be published, we'll look at how to do edits and deletes, paging and sorting, and utilize custom Button actions with the DataList and Repeater controls.
Also, a word of thanks to the many people who've read the tutorials. I've been getting about a dozen emails a day from folks with questions, pointing out typos, comments, and so on, and have tried my best to respond as quickly as possible (although some replies have taken several days - sorry!).
My tenth Toolbox column in the October 2006 issue of MSDN Magazine is now avaiable online. The October issue examines two products:
- PreEmtive Solutions Dotfuscator (version 3.0) - obfuscate your .NET assemblies to protect your company's intellectual property. Obfuscation (IMO) is a must if you develop a product that is shipped to customers.
- NHibernate - quickly persist and retrieve information from a database using NHibernate to automate the process of building the necessary code and query capabilities. NHibernate is a free, open-source framework tool for quickly building a data access layer between your business objects and data store.
This month's issue reviewed Eric Brown's book SQL Server 2005 Distilled. Eric provides a nice high-level overview of the key features and changes in SQL Server 2005 that is a must-read for any organization that's contemplating upgrading to 2005. From the review:
Most computer trade books focus on exploring a particular subset of features in great depth and are ideal for developers interested in mastering a new technology. If, however, you want a high-level overview of a technology suitable for evaluation purposes (or for just gaining a broad understanding of the technology) you'll instead need to turn to a book like SQL Server 2005 Distilled (Addison-Wesley, 2006). Authored by former SQL Server team member Eric Brown, the book provides an executive summary of key features, such as security, database management, developer tools, business intelligence, working with XML data, CLR integration, and so on.
Apart from Chapter 6, "The Code Chapter," which has a hodgepodge of examples for performing various queries and administrative tasks, you won't find much T-SQL syntax or DBA-level walkthroughs. Instead, the other chapters provide their high-level overviews using mostly prose mixed with architectural and use case diagrams. And at 336 pages, SQL Server 2005 Distilled provides a digestible overview that can be read cover-to-cover over the course of a weekend.
As always, if you have any suggestions for products or books to review for the Toolbox column, please send them into firstname.lastname@example.org
You can keep abreast of the latest Toolbox articles through the column's RSS feed or the Toolbox column category here on my blog.