This is a migrated thread and some comments may be shown as answers.

Unable to find control to attach to Validator

2 Answers 255 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
paul
Top achievements
Rank 1
paul asked on 14 Oct 2011, 03:24 PM
Hello,

I have created a composite control with the RadDatePicker and 2 CompareValidators and 1 CustomValidator.  When I try to attach the RadDatePicker to a RequiredFieldValidator from the calling page i am getting the Error

Unable to find control id 'calendarControl_CtcRadDatePicker' referenced by the 'ControlToValidate' property of 'reqValidator'.


Control
/// <summary>
    /// Summary description for CalendarControl
    /// </summary>
    [ToolboxData("<{0}:CalendarControl runat=server></{0}:CalendarControl>")]
    [ValidationProperty("SelectedDate")]
    public class CalendarControl : CompositeControl
    {
        private const string DEFAULT_SKIN = "CentreTelerik";

        private RadDatePicker _radDatePicker;
        private CompareValidator _cpvMaxDateOccurred;
        private CompareValidator _cpvMinDateOccurred;
        private CustomValidator _custValidator;

        public CalendarControl()
        {

        }

        #region Instance properties

        public DateTime MinDate
        {
            get { return (DateTime)(ViewState["CtcMinDate"] ?? _radDatePicker.MinDate); }
            set
            {
                ViewState["CtcMinDate"] = value;
                EnsureChildControls();
                _radDatePicker.MinDate = value;
            }
        }

        public DateTime MaxDate
        {
            get { return (DateTime)(ViewState["CtcMaxDate"] ?? _radDatePicker.MaxDate); }
            set
            {
                ViewState["CtcMaxDate"] = value;
                EnsureChildControls();
                _radDatePicker.MaxDate = value;
            }
        }

        public DateTime SelectedDate
        {
            get { EnsureChildControls(); return _radDatePicker.SelectedDate.GetValueOrDefault(); }
            set { EnsureChildControls(); _radDatePicker.SelectedDate = value; }
        }

        public string ClientIDWrapper
        {
            get { EnsureChildControls(); return _radDatePicker.ClientID; }
        }

        #endregion

        #region Override Instance methods

        protected override ControlCollection CreateControlCollection()
        {
            EnsureChildControls();
            return base.CreateControlCollection();
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            base.CreateChildControls();

            _radDatePicker = new RadDatePicker();
            _radDatePicker.ID = "CtcRadDatePicker";
            _radDatePicker.EnableEmbeddedSkins = false;
            _radDatePicker.Calendar.EnableEmbeddedSkins = false;
            _radDatePicker.Calendar.ShowRowHeaders = false;
            _radDatePicker.DateInput.EnableEmbeddedSkins = false;

            _cpvMaxDateOccurred = new CompareValidator();
            _cpvMaxDateOccurred.ID = "CtcMaxValidator";
            _cpvMaxDateOccurred.Type = ValidationDataType.Date;
            _cpvMaxDateOccurred.Operator = ValidationCompareOperator.LessThanEqual;
            _cpvMaxDateOccurred.Display = ValidatorDisplay.Dynamic;
            _cpvMaxDateOccurred.ControlToValidate = _radDatePicker.ClientID;
            _cpvMaxDateOccurred.ValueToCompare = _radDatePicker.MaxDate.ToShortDateString();
            _cpvMaxDateOccurred.SetFocusOnError = true;

            _cpvMinDateOccurred = new CompareValidator();
            _cpvMinDateOccurred.ID = "CtcMinValidator";
            _cpvMinDateOccurred.Type = ValidationDataType.Date;
            _cpvMinDateOccurred.Operator = ValidationCompareOperator.GreaterThanEqual;
            _cpvMinDateOccurred.Display = ValidatorDisplay.Dynamic;
            _cpvMinDateOccurred.ControlToValidate = _radDatePicker.ClientID;
            _cpvMinDateOccurred.ValueToCompare = _radDatePicker.MinDate.ToShortDateString();
            _cpvMinDateOccurred.SetFocusOnError = true;

            _custValidator = new CustomValidator();
            _custValidator.ID = "CtcCustomValidatorInvalidDate";
            _custValidator.ControlToValidate = _radDatePicker.ClientID;
            _custValidator.Display = ValidatorDisplay.Dynamic;
            _custValidator.ErrorMessage = "Invalid Date";

            Controls.Add(_radDatePicker);
            Controls.Add(_cpvMaxDateOccurred);
            Controls.Add(_cpvMinDateOccurred);
            Controls.Add(_custValidator);

            FooterTemplate template = new FooterTemplate(_radDatePicker.ClientID, "Today", !IsTodayDisabled());
            _radDatePicker.Calendar.FooterTemplate = template;

            
        }

        protected override void OnDataBinding(EventArgs e)
        {
            EnsureChildControls();
            base.OnDataBinding(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            _radDatePicker.Skin = DEFAULT_SKIN;
            _radDatePicker.DateInput.Skin = DEFAULT_SKIN;
            _radDatePicker.Calendar.Skin = DEFAULT_SKIN;
            _radDatePicker.Enabled = Enabled;
            _radDatePicker.DateInput.ClientEvents.OnError = "CalendarControlOnError";
            _radDatePicker.DatePopupButton.ToolTip = ToolTip;

            _cpvMaxDateOccurred.Enabled = Enabled;
            _cpvMaxDateOccurred.ValueToCompare = _radDatePicker.MaxDate.ToShortDateString();
            _cpvMaxDateOccurred.ErrorMessage = "Max date error";

            _cpvMinDateOccurred.Enabled = Enabled;
            _cpvMinDateOccurred.ValueToCompare = _radDatePicker.MinDate.ToShortDateString();
            _cpvMinDateOccurred.ErrorMessage = "Min date error";

            _custValidator.ClientValidationFunction = "CalendarControlClientValidationFunction";
            _custValidator.ValidateEmptyText = true;

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            _radDatePicker.RenderControl(writer);
            _cpvMaxDateOccurred.RenderControl(writer);
            _cpvMinDateOccurred.RenderControl(writer);
            _custValidator.RenderControl(writer);
        }
        #endregion

        #region Private methods
        private bool IsTodayDisabled()
        {
            bool todayDisabled = false;
            foreach (RadCalendarDay disabled in _radDatePicker.Calendar.SpecialDays)
            {
                if (disabled.IsToday)
                {
                    todayDisabled = true;
                    break;
                }
            }
            return todayDisabled;
        }
        #endregion


        sealed class FooterTemplate : ITemplate
        {
            private string _clientId;
            private string _todayText;
            private bool _enabled;

            #region ITemplate Members
            public FooterTemplate(string clientId, string todayText, bool enabled)
            {
                _clientId = clientId;
                _todayText = todayText;
                _enabled = enabled;
            }

            public void InstantiateIn(Control container)
            {
                RadCodeBlock block = new RadCodeBlock();
                block.ID = "footerCodeBlock";

                LiteralControl link = new LiteralControl();
                link.ID = "todayLink";
                link.DataBinding += new EventHandler(link_DataBinding);
                StringBuilder text = new StringBuilder();
                text.Append("<div style=\"text-align:center; width:100%;\">");
                if (_enabled)
                {
                    text.AppendFormat("<a href=\"\" onclick=\"javascript: var picker = $find('{0}');  picker.set_selectedDate(new Date());  picker.hidePopup(); return false;\">{1}</a>", _clientId, _todayText);
                }
                else
                {
                    text.AppendFormat("{0}", _todayText);
                }
                text.Append("</div>");
                link.Text = text.ToString();
                block.Controls.Add(link);

                container.Controls.Add(block);
            }

            void link_DataBinding(object sender, EventArgs e)
            {
                LiteralControl literalControl = (LiteralControl)sender;
                DataListItem list = (DataListItem)literalControl.NamingContainer;
            }
            #endregion
        }
    }
-------------------------------------------------
Calling Page
form id="form1" runat="server">
    <div>
    
        <script type="text/javascript">
        var invalidInput = false;

        function CalendarControlOnError(sender, args) {
            
            sender.updateCssClass();   
            if (args.get_reason() == Telerik.Web.UI.InputErrorReason.OutOfRange) {
                args.set_cancel(true);
                event.cancelBubble = true;
            }
            else {
                //args.set_cancel(true);
                //event.cancelBubble = true;
                invalidInput = true;
            }
        }

        function CalendarControlClientValidationFunction(sender, args) {
            if (invalidInput)
                args.IsValid = false;
            invalidInput = false;
        }
        </script>
        
        <telerik:RadScriptManager ID="radScriptManager" runat="server" />
        <centre:CalendarControl ID="calendarControl" runat="server" />
        
        <asp:RequiredFieldValidator ID="reqValidator" runat="server" ErrorMessage="Required" />
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save_OnClick" />
    </div>
-----------------------------------------------
code behind
protected override void OnInit(EventArgs e)
        {
            reqValidator.ControlToValidate = calendarControl.ClientIDWrapper;
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                calendarControl.MaxDate = DateTime.Today;
                calendarControl.SelectedDate = DateTime.Today;
            }
        }

        protected void Save_OnClick(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;
        }

2 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 14 Oct 2011, 08:32 PM
Hello Paul,

In your ClientIDWrapper, you need to return the ID, not the ClientID, since that is what the CustomValidator binds to.

I hope that helps.
0
paul
Top achievements
Rank 1
answered on 14 Oct 2011, 09:30 PM
I still get the same error returning the ID
Tags
Calendar
Asked by
paul
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
paul
Top achievements
Rank 1
Share this question
or