Article ID: 310145 - Last Review: May 29, 2007 - Revision: 4.2 How to extend a Web form control to work with the validation controls by using Visual C#
This article was previously published under Q310145 On This PageSUMMARY
This step-by-step article shows you how to extend the Calendar control for server-side validation.
Not all Web form controls can be used in conjunction with the validation controls. In order to be used with the validation controls, the control must have a ValidationProperty attribute. The ValidationProperty attribute tells the validation controls the value to validate against. The Calendar control is one of the controls that cannot be validated in its original state. In many cases, however, it is necessary to be able to validate the user selection in the Calendar control. There are two solutions to this problem. The first is to write a custom validation control. The second is to extend the Calendar control so that it can be used with the built-in validation controls. This article addresses the second solution. For information on writing a custom validation control, refer to the "References" section of this article. NOTE: This article shows you how to extend the Calendar control for server-side validation only. For client-side validation to occur, the validation controls hook up to the corresponding HTML control's Value property. Because the Calendar control is built through the use of many HTML elements, there is no single HTML control that contains the selected value for the Calendar control, so client-side validation is not appropriate for the Calendar control. Extending the Calendar ControlYou can extend the Calendar control by creating a class that inherits from the existing Calendar control. To allow the control to interact with the validation controls, you add the ValidationProperty attribute and a property that returns the selected date in a format suitable for the validation controls.To allow the control to be used with the RequiredFieldValidator, your code will return an empty string if the Calendar's SelectedDate property is set to 01-Jan-0001 because this is the date that is returned if no date is selected. In all other cases, it will return a string that contains the date formatted as yyyy/MM/dd, which can be used by the RandgeValidator control.
Adding the Control to a Web FormIn order to add a custom control to a Web form, you must add a reference to the top of the Web page. This defines the namespace and tag you use in the HTML.
Linking to Validator ControlsThis step adds both a RangeValidator and a RequiredFieldValidator control to the Web form and links them to the VCalendar control.
Testing the Web Page
REFERENCES
See the Help topic "Web Forms Validation". You can look it up by typing this in the Help Search tool and limiting the search to Titles Only. | Other Resources Other Support Sites
CommunityGet Help NowArticle Translations
|






Windows Live
Facebook
Twitter
Linkedin
Digg it
Yahoo
Delicious
StumbleUpon
Yammer
Reddit
Technorati
FriendFeed
Email
Back to the top
