Custom ServerControl inherited from RadListbox with requiredfieldvalidator not working

1 posts, 0 answers
  1. Vinayak
    Vinayak avatar
    1 posts
    Member since:
    Sep 2011

    Posted 15 Sep 2011 Link to this post

    Hi,

    I am developing Custom server control(Ex : MyRadListBox) by inheriting from RadListBox. And I need RequiredFieldValidator inside the MyRadListBox class. Means need a RequiredFieldvalidator feature with My Custom server control. But its giving error like "Unable to find control id 'MyListBox1' referenced by the 'ControlToValidate' property of ''.

    My Code is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI;
     
    namespace WebApplication1
    {
        public class MyListBox : RadListBox
        {
            RequiredFieldValidator rfv ;
            protected override void CreateChildControls()
            {
                rfv = new RequiredFieldValidator();
                rfv.ControlToValidate = this.ID;
                rfv.ErrorMessage = "This is required";
                rfv.EnableClientScript = true;
                base.Controls.Add(rfv);
                base.CreateChildControls();
            }
     
            protected override void Render(HtmlTextWriter writer)
            {
                base.Render(writer);
                rfv.RenderControl(writer);
            }
        }
    }

    But If I inherit from simple ASP Listbox it works fine. Look at below code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI;
     
    namespace WebApplication1
    {
        public class MyListBox : ListBox
        {
            RequiredFieldValidator rfv ;
            protected override void CreateChildControls()
            {
                rfv = new RequiredFieldValidator();
                rfv.ControlToValidate = this.ID;
                rfv.ErrorMessage = "This is required";
                rfv.EnableClientScript = true;
                base.Controls.Add(rfv);
                base.CreateChildControls();
            }
     
            protected override void Render(HtmlTextWriter writer)
            {
                base.Render(writer);
                rfv.RenderControl(writer);
            }
        }
    }

    I am not getting why its not working with RadListBox and why its working with normal asp ListBox control?

    Please kindly help me to resolve this problem.

    Thanks
    Vinayak


Back to Top