03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

320 x CHAPTER 9 VALIDATING USER INPUT<br />

Because you can’t rely on these HTML5 attributes to validate your data, it’s important that you use<br />

the <strong>ASP</strong>.<strong>NET</strong> validation controls. The next section discusses the rema<strong>in</strong><strong>in</strong>g validation controls that<br />

are available <strong>in</strong> <strong>ASP</strong>.<strong>NET</strong>.<br />

The St<strong>and</strong>ard Validation Controls<br />

The five validation controls (the ones <strong>in</strong> the Validation category of the Toolbox whose names end<br />

<strong>in</strong> Validator) ultimately all <strong>in</strong>herit from the same base class, <strong>and</strong> thus share some common behavior.<br />

Four of the five validation controls operate <strong>in</strong> the same way, <strong>and</strong> conta<strong>in</strong> built-<strong>in</strong> behavior that<br />

enables you to validate associated controls. The last control, the CustomValidator, enables you to<br />

write custom validation rules not supported out of the box.<br />

The follow<strong>in</strong>g table lists a number of common properties that are shared by the validation controls<br />

<strong>and</strong> that you typically use when work<strong>in</strong>g with them.<br />

PROPERTY<br />

Display<br />

CssClass<br />

ErrorMessage<br />

Text<br />

ControlToValidate<br />

EnableClientScript<br />

SetFocusOnError<br />

DESCRIPTION<br />

This property determ<strong>in</strong>es whether or not the hidden error message takes<br />

up space. With the Display set to Static, the error message takes up<br />

screen real estate, even when it is hidden. This is similar to the CSS sett<strong>in</strong>g<br />

visibility: hidden you saw <strong>in</strong> earlier chapters. The Dynamic<br />

sett<strong>in</strong>g hides the error message us<strong>in</strong>g display: none until it needs to<br />

be displayed. With a sett<strong>in</strong>g of None, the control is not visible at all. This is<br />

useful if you are us<strong>in</strong>g a ValidationSummary, which you see later <strong>in</strong> this<br />

chapter.<br />

This property enables you to set the CSS class attribute that is applied to<br />

the error message text.<br />

This property holds the error message used <strong>in</strong> the ValidationSummary<br />

control. When the Text property is empty, the ErrorMessage value is<br />

also used as the text that appears on the page.<br />

The Text property is used as the text that the validation control displays<br />

on the page. This could be an asterisk (*) to <strong>in</strong>dicate an error, or text like<br />

“Enter your name.”<br />

This property conta<strong>in</strong>s the server ID of the control that needs to be<br />

validated.<br />

This property determ<strong>in</strong>es whether the control provides validation at the<br />

client. The default is True.<br />

This property determ<strong>in</strong>es whether client-side script gives the focus to the<br />

first control that generated an error. This sett<strong>in</strong>g is False by default.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!