views:

23

answers:

0

Hello All:

I am creating a custom control to extend textbox to include validations.

Following in the code:

  public class EncodedTextBox:TextBox
    {
        /// <summary>
        /// 
        /// </summary>
        private RequiredFieldValidator rqvVldrText;
        /// <summary>
        /// 
        /// </summary>
        private RegularExpressionValidator regVldrText;
        [Bindable(false), Category("Appearance"), DefaultValue(""), Description(""), Browsable(true)]
        public string InvalidMessageForRFV { get; set; }

          [Bindable(false), Category("Appearance"), DefaultValue(""), Description(""), Browsable(true)]
        public string InvalidMessageForRegexValidator { get; set; }


        [Bindable(false), Category("Appearance"), DefaultValue(""), Description(""), Browsable(true)]
        public string ErrorMessageCss { get; set; }

        [Bindable(false), Category("Appearance"), DefaultValue("true"), Description(""), Browsable(true)]
        public bool ClientScript { get; set; }

        [Bindable(false), Category("Appearance"), DefaultValue(""), Description(""), Browsable(true)]
        public string RegularExpressionName { get; set; }

        [Bindable(false), Category("Appearance"), DefaultValue(""), Description(""), Browsable(true)]
        public string CompleteValidationGroup { get; set; }

        protected override void OnInit(EventArgs e)
        {
                rqvVldrText = new RequiredFieldValidator();
                rqvVldrText.ID = "RequiredFieldValidator" + this.ID;
                rqvVldrText.ControlToValidate = this.ID;
                rqvVldrText.ErrorMessage = this.InvalidMessageForRFV;
                rqvVldrText.CssClass = this.ErrorMessageCss;
                rqvVldrText.EnableClientScript = this.ClientScript;
                rqvVldrText.ValidationGroup = this.CompleteValidationGroup;
                Controls.Add(rqvVldrText);

                    regVldrText = new RegularExpressionValidator();
                    regVldrText.ID = "RegularExpressionValidator"+this.ID;
                    regVldrText.ControlToValidate = this.ID;
                    regVldrText.ErrorMessage = this.InvalidMessageForRegexValidator;
                    regVldrText.CssClass = this.ErrorMessageCss;
                    regVldrText.EnableClientScript = this.ClientScript;
                    regVldrText.ValidationGroup = this.CompleteValidationGroup;
                    Controls.Add(regVldrText);
                }

        protected override void Render(HtmlTextWriter w)
        {
            base.Render(w);
            if (rqvVldrText != null)
            {
                rqvVldrText.RenderControl(w);
            }
            if (regVldrText != null)
            {
                regVldrText.RenderControl(w);
            }
        }
}

This works fine. But I read that we need to implement INamingContainer interface for proper functioning of the control. But when I inherit the control from INamingContainer I get following error

Unable to find control id 'EncodedTextBox2' referenced by the 'ControlToValidate' property of 'RequiredFieldValidatorEncodedTextBox2'. 

Can anybody tell me how to fix this? And I am facing one more issue. When I have one control of this type on page then in design view it is displayed perfectly, but when I include more text boxes then except for the first one, all the other textboxes show rendering error. Is this because of same error which I am facing by using INamingContainer?

Thanks Ashwani