Can someone suggest how to implement a custom validator for a Web Forms Calendar control? Apparently, neither RequiredValidator nor CustomValidator work out of the box with
I think you should use Ajax Control Toolkit (downloadale from: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/) Then you can format your calendar as you want. An example is like this:
<ajaxToolkit:Calendar runat="server"
TargetControlID="Date1"
CssClass="ClassName"
Format="MMMM d, yyyy"
PopupButtonID="Image1" />
If you need to make sure that a date has been chosen, then you can do the following:
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
ID="PNReqE"
TargetControlID="Date1"
Width="350px"
HighlightCssClass="highlight"
CssClass="CustomValidatorCalloutStyle"
PopupPosition="Right"
WarningIconImageUrl="warning.gif"
CloseImageUrl="close.gif" />
I finally got it to work this way:
<asp:Calendar ID="startCalendar" CssClass="startDate"
OnSelectionChanged="Selection_Changed" runat="server"></asp:Calendar>
<asp:CustomValidator ID="dateCustVal" OnServerValidate="DateCustVal_Validate"
runat="server"></asp:CustomValidator>
protected void DateCustVal_Validate(object source, ServerValidateEventArgs args)
{
if (startCalendar.SelectedDate == null
|| startCalendar.SelectedDate == new DateTime(0001, 1, 1, 0, 0, 0))
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}