Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
140
Custom WebDatePicker with an embedded RequiredFieldValidator
posted

Hello,

I want to create a custom WebDatePicker with an embedded RequiredFieldValidator... Here is my code:

System.Web.UI.WebControls.RequiredFieldValidator rfv;

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            rfv = new System.Web.UI.WebControls.RequiredFieldValidator();

            rfv.ControlToValidate = this.ID;

            rfv.ID = "rfv_" + this.ID;

            rfv.ErrorMessage = "Error message";

            rfv.Text = "*";

 

            this.Controls.Add(rfv);

        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)

        {

            base.Render(writer);

            rfv.RenderControl(writer);

        }

 

But I have this error:

Unable to find control id 'Cdatepicker1' referenced by the 'ControlToValidate' property of 'toto_Cdatepicker1'.

I have tried this solution:

http://forums.infragistics.com/forums/p/50289/264449.aspx

        System.Web.UI.WebControls.RequiredFieldValidator rfv;

 

        protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

 

            this.Page.LoadComplete += new EventHandler(Page_LoadComplete);

        }

 

        void Page_LoadComplete(object sender, EventArgs e)

        {

            rfv = new System.Web.UI.WebControls.RequiredFieldValidator();

            //cv.ControlToValidate = this.UniqueID;

            rfv.ID = "rfv_" + this.ID;

            rfv.ErrorMessage = "Message erreur";

            rfv.Text = "*";

            rfv.ControlToValidate = this.UniqueID;

 

            this.Page.Form.Controls.Add(rfv);

        }

 

It's work but my field validator is at the bottom of my page...

How can I put it next my control ?

 

Thanks,

 

Antonio

 

 

 

Parents
No Data
Reply
  • 49378
    posted

    Hi Antonio,

    Thank you for posting in the community.

    Using the second approach you have described, I suggest that you add the validator to the form controls using the AddAt method to add the validator at a specified index. For instance:

     

     

     

    this.Page.Form.Controls.AddAt(4, rfv);

    Please let me know if this helps.

Children