Summarizing Validation Messages
As you may have noticed, all the validation messages take up a lot of space
on the page梱ou may not want to sacrifice all that space for your
validation messages. ASP.NET provides its ValidationSummary control so that
you can display all the validation messages in one place as well as display
either nothing at all or a simple indicator next to each invalid control.
The sample page, shown in Figure 8.3, displays an asterisk next to each
invalid control and full error message text in the summary.
Figure 8.3. The ValidationSummary control lists all the ErrorMessage
properties for invalid controls in one place.
Adding the ValidationSummary control is simple: All you have to do is place
the control on your page. It automatically seeks out all the various
validation controls on the page, gathering the ErrorMessage property from
each control as necessary.
To set the text to be displayed within the validation controls themselves,
set the Text property for each control. This property overrides the
ErrorMessage property, and each control uses the Text property to determine
what to show within its own display. (If you don't specify the Text
property, as has been the case throughout this chapter, the controls
display their ErrorMessage property.) To add the ValidationSummary control
to your page, follow these steps:
Select the first validation control on the page and then Shift+click each
of the remaining validation controls, selecting them all.
In the Properties window, set the Text property (for all the selected
controls) to *.
Click directly above the Save button to place the insertion point within
the page. In the Toolbox window, double-click the ValidationSummary control
to insert an instance of this control.
Browse the page and then click Save without entering any data. Try entering
invalid data in some of the controls. When you submit the page, you should
see asterisks next to the invalid controls and a summary of the errors at
the bottom of the page.
TIP
If you want to modify the layout and behavior of the ValidationSummary
control, check out the DisplayMode property, which can be set to List,
BulletList, or Paragraph. Try them all to see how they affect the display.
In addition, you might check out the ShowMessageBox property, which allows
you to have up-level browsers (such as Internet Explorer) display a message
box alert containing the validation information.
As you may have noticed, all the validation messages take up a lot of space
on the page梱ou may not want to sacrifice all that space for your
validation messages. ASP.NET provides its ValidationSummary control so that
you can display all the validation messages in one place as well as display
either nothing at all or a simple indicator next to each invalid control.
The sample page, shown in Figure 8.3, displays an asterisk next to each
invalid control and full error message text in the summary.
Figure 8.3. The ValidationSummary control lists all the ErrorMessage
properties for invalid controls in one place.
Adding the ValidationSummary control is simple: All you have to do is place
the control on your page. It automatically seeks out all the various
validation controls on the page, gathering the ErrorMessage property from
each control as necessary.
To set the text to be displayed within the validation controls themselves,
set the Text property for each control. This property overrides the
ErrorMessage property, and each control uses the Text property to determine
what to show within its own display. (If you don't specify the Text
property, as has been the case throughout this chapter, the controls
display their ErrorMessage property.) To add the ValidationSummary control
to your page, follow these steps:
Select the first validation control on the page and then Shift+click each
of the remaining validation controls, selecting them all.
In the Properties window, set the Text property (for all the selected
controls) to *.
Click directly above the Save button to place the insertion point within
the page. In the Toolbox window, double-click the ValidationSummary control
to insert an instance of this control.
Browse the page and then click Save without entering any data. Try entering
invalid data in some of the controls. When you submit the page, you should
see asterisks next to the invalid controls and a summary of the errors at
the bottom of the page.
TIP
If you want to modify the layout and behavior of the ValidationSummary
control, check out the DisplayMode property, which can be set to List,
BulletList, or Paragraph. Try them all to see how they affect the display.
In addition, you might check out the ShowMessageBox property, which allows
you to have up-level browsers (such as Internet Explorer) display a message
box alert containing the validation information.
ASP.NET中ValidationSummary控件的使用
ASP.NET的ValidationSummary控件可将所有验证消息集中显示,节省页面空间。添加该控件很简单,只需将其放置在页面上,它会自动收集各验证控件的错误消息。还可设置Text属性,修改显示内容。此外,可通过DisplayMode和ShowMessageBox属性调整布局和行为。
2057

被折叠的 条评论
为什么被折叠?



