You have great control over how validation errors are reported. For example, rather than putting error messages alongside the control, you can summarize all the validation failures with a ValidationSummary control. This control can place a summary of the errors in a bulleted list, a simple list, or a paragraph that appears on the web page or in a pop-up message box. Add a ValidationSummary control at the bottom of the page (after the table). There are a few properties to set in the design view. Set the id to valSum (that becomes the ID of the validation summary control). Next set the DisplayMode by clicking on the Display Mode property. Notice that the various valid display modes are displayed in a drop-down list, as shown in Figure 9-8. Interestingly, if you choose to set these attributes by hand, in Source Mode, IntelliSense helps you with the valid Display Modes as well, as shown in Figure 9-9. You can leave the default, BulletList, for now. In addition to choosing among a BulletList, List, or SingleParagraph format for displaying the list of errors, you must decide on whether to show a summary at the bottom of the page and/or a pop-up message box. You configure this using the ShowMessageBox and ShowSummary properties, as shown in Figure 9-10. Set ShowMessageBox to true and ShowSummary to False for now. The HeaderText property holds the header that will be displayed if there are errors to report. Set it to The following errors were found:.
Figure 9-8. Set the Summary Display ModeFigure 9-9. Set the Summary Display Mode in sourceTo make the summary validator work as expected, you'll need to add an ErrorMessage attribute to the other validation controls. Click on the first validation control (reqFieldBooks) and in its properties, set its ErrorMessage to You did not pick a book. This value will now be displayed in the summary, and need not be displayed next to the incorrect value. Thus you can change the Text property from You did not pick a book to an asterisk: "*". You'll want to do the equivalent for the other three validators. Figure 9-10. Show Summary propertyRather than choose which of the three types of summary reports (bulleted list, list, or summary paragraph) to provide, for this example you'll let the user choose from a drop-down list. To do so, add a row to the table, right above the Submit Bug button. In the first column, add the title Display Report. Set its align property to right. In the second column, drag in a DropDownList (id=ddlFormat) and set its AutoPostBack property to true. Finally, add three items to the drop down's items list: List, BulletList, and SingleParagraph. Set the event handler for SelectedIndexChanged by clicking on the lightning bolt in the Properties window, then double-clicking on the space next to the SelectedIndexChanged event. When you double-click on the event handler, it is given a name, and you are placed in the code editor to implement the event handler. It just sets the DisplayMode of the summary validator. Protected Sub ddlFormat_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) ValSum.DisplayMode = _ CType(ddlFormat.SelectedIndex, _ ValidationSummaryDisplayMode) End Sub
Similarly, you'll add a second drop-down list to allow the user to control whether the error report appears in the page or in a pop-up menu. Add another row to the table, right above the row you just created. In the first column, type the prompt Display Errors. Set its align property to right. Drag a drop-down list into the second column. Set its ID to ddlDisplay. Populate it with the two choices, Summary and Message Box. Remember to set its AutoPostback property to TRue, and implement its SelectedIndexChanged handler as follows: Protected Sub ddlDisplay_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) If ddlDisplay.SelectedIndex = 0 Then ValSum.ShowSummary = True ValSum.ShowMessageBox = False Else ValSum.ShowSummary = False ValSum.ShowMessageBox = True End If End Sub Note that the decision to use a summary or a message box and the display mode, are set in event handlers. If the user clicks the button before changing either of these drop-down settings, default values will be used. There are many ways to solve this problem, including using member variables to hold the state, but a simple solution is to invoke these event handlers programmatically when the page is loaded. Protected Sub Page_Load( _ ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ddlFormat_SelectedIndexChanged(sender, e) ddlDisplay_SelectedIndexChanged(sender, e) End Sub Build and run the application. If you click on Submit Bug without setting anything, the default values of BulletList and summary will be used, as shown in Figure 9-11. If you change Summary to Message Box and BulletList to List, a message box is shown rather than the summary, as shown in Figure 9-12. |