Calendar Custom Validator

前端 未结 2 1062
温柔的废话
温柔的废话 2020-12-20 08:11

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

相关标签:
2条回答
  • 2020-12-20 08:33

    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" />
    
    0 讨论(0)
  • 2020-12-20 08:40

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题