Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:05:58+00:00 2026-06-16T07:05:58+00:00

Alright, this is a little bit complicated. I have a composite control that contains

  • 0

Alright, this is a little bit complicated.

I have a composite control that contains textbox, required field validator and a button.
It is exposing a property called ValidationGroup.

It applies the ValidationGroup property to textbox , required field validator and the button.

Everything works fine.

The problem begins when I take out the button and place it out in the form with the same validation group.

Ideally it should have worked. But it doesn’t.
Some say that composite controls have their own naming container and so this will not work.

If that is true, I don’t understand why ? I mean they have the same validation group!

anyways, does anyone know how to fix this ?

here’s my code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FCC.Web.UI.CustomControl;
using System.Xml;

namespace FCC.Web.UI.CustomControl
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("Value")]
    [ValidationProperty("Value")]
    [ToolboxData("<{0}:ValidationTextBox runat=server></{0}:ValidationTextBox>")]
    public class ValidationTextBox : CompositeControl, IDynamicControl
    {

        #region-- privatess --
        private TextBox textBox1;
        private RequiredFieldValidator requiredFieldValidator1;
        private RegularExpressionValidator regularExpressionValidator1, maxlengthValidator,integerValidator,decimalValidator;
        private string validationClasses, validationGroup, validationExpression, xmlElementName;
        private bool isRequired;
        private int maxlength;
        #endregion

        #region-- properties -- The following properties are deleted to child controls

        #region-- textbox --

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Text displayed on the textbox")]
        public string Value
        {
            get
            {
                EnsureChildControls();
                return textBox1.Text;
            }
            set
            {
                EnsureChildControls();
                textBox1.Text = value;
            }
        }

        [Bindable(true)]
        [Category("Styles")]
        [Themeable(true)]
        [DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Localizable(true)]
        public Style TextBoxStyle
        {
            get
            {
                EnsureChildControls();
                return textBox1.ControlStyle;
            }
            set
            {
                EnsureChildControls();
                //textBox1.Attributes["style"] = value;
                textBox1.ControlStyle.MergeWith(value);
            }
        }

        [Bindable(true)]
        [Category("Styles")]
        [Localizable(true)]
        public string TextBoxCssClass
        {
            get
            {
                EnsureChildControls();
                return textBox1.CssClass;
            }
            set
            {
                EnsureChildControls();
                textBox1.CssClass = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public TextBoxMode TextMode
        {
            get
            {
                EnsureChildControls();
                return textBox1.TextMode;
            }
            set
            {
                EnsureChildControls();
                textBox1.TextMode = value;
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public int MaxLength
        {
            get
            {
                EnsureChildControls();
                return maxlength;
            }
            set
            {
                EnsureChildControls();

                if (value > 0)
                {
                    maxlength = value;

                    textBox1.MaxLength = value;

                    maxlengthValidator.Enabled = true;

                    //maxlengthValidator.ValidationExpression = "^[a-zA-Z.]{0," + value + "}$";
                    maxlengthValidator.ValidationExpression = @"^[\s\S]{0," + value + "}$";
                    maxlengthValidator.ErrorMessage = "Maximum allowed charaters: " + value;
                }
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool IsInteger
        {
            get
            {
                EnsureChildControls();
                return integerValidator.Enabled;
            }
            set
            {
                EnsureChildControls();

                if (value == true) // Is Integer
                {
                    integerValidator.Enabled = true;
                    regularExpressionValidator1.Enabled = false;
                    maxlengthValidator.Enabled = false;
                    decimalValidator.Enabled = false;
                }
                else
                {
                    integerValidator.Enabled = false;
                }
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Error message that appears when textbox contain invalid integer")]
        public string InvalidIntegerErrorMessage
        {
            get
            {
                EnsureChildControls();
                return integerValidator.ErrorMessage.IsNullOrEmpty() ? "Invalid Number" : integerValidator.ErrorMessage;
            }
            set
            {
                EnsureChildControls();
                integerValidator.ErrorMessage = value;
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool IsDecimal
        {
            get
            {
                EnsureChildControls();
                return decimalValidator.Enabled;
            }
            set
            {
                EnsureChildControls();
                if (value == true)
                {
                    decimalValidator.Enabled = true;
                    integerValidator.Enabled = false;
                    regularExpressionValidator1.Enabled = false;
                    maxlengthValidator.Enabled = false;
                }
                else
                {
                    decimalValidator.Enabled = false;
                }
            }
        }


        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Error message that appears when textbox contain invalid decimal number")]
        public string InvalidDecimalErrorMessage
        {
            get
            {
                EnsureChildControls();
                return decimalValidator.ErrorMessage.IsNullOrEmpty() ? "Invalid Number" : decimalValidator.ErrorMessage;
            }
            set
            {
                EnsureChildControls();
                decimalValidator.ErrorMessage = value;
            }
        }


        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Name of XmlElement")]
        public string XmlElementName
        {
            get
            {
                EnsureChildControls();
                return xmlElementName;
            }
            set
            {
                EnsureChildControls();
                xmlElementName = value;
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public string XmlElements
        {
            get
            {
                EnsureChildControls();
                return string.Format("<{0}>{1}</{0}>",XmlElementName,textBox1.Text);
            }
        }

        #endregion

        #region--required validation --

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Error message that appears when text box is empty")]
        public String RequiredErrorMessage
        {
            get
            {
                EnsureChildControls();
                return requiredFieldValidator1.ErrorMessage.IsNullOrEmpty() ? "Required Field" : requiredFieldValidator1.ErrorMessage;
            }
            set
            {
                EnsureChildControls();
                requiredFieldValidator1.ErrorMessage = value;
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [Localizable(true)]
        [Description("bool, representing text in the textbox is required field or not")]
        public bool IsRequired
        {
            get
            {
                EnsureChildControls();
                return isRequired;
            }
            set
            {
                EnsureChildControls();
                isRequired = value;
                requiredFieldValidator1.Enabled = value;
            }
        }

        #endregion

        #region-- regular expression validation --

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Error message that appears when textbox contain invalid format string")]
        public String RegularExpressionErrorMessage
        {
            get
            {
                EnsureChildControls();
                return regularExpressionValidator1.ErrorMessage.IsNullOrEmpty() ? "Invalid Text" : regularExpressionValidator1.ErrorMessage;
            }
            set
            {
                EnsureChildControls();
                regularExpressionValidator1.ErrorMessage = value;
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Regular expression. Leave blank if you dont want to implement.")]
        public string RegularExpression
        {
            get
            {
                EnsureChildControls();
                return validationExpression;
            }
            set
            {
                EnsureChildControls();

                validationExpression = value;
                regularExpressionValidator1.ValidationExpression = value;
                regularExpressionValidator1.Enabled = true;

                integerValidator.Enabled = false;
                decimalValidator.Enabled = false;
            }
        }

        #endregion

        #region--common--
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("cssClasses that apply to all validators")]
        public string ValidationErrorCssClass
        {
            get
            {
                EnsureChildControls();
                return validationClasses;
            }
            set
            {
                EnsureChildControls();
                validationClasses = value;

                requiredFieldValidator1.CssClass = value;
                regularExpressionValidator1.CssClass = value;
                maxlengthValidator.CssClass = value;
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        [Description("Validation group for the textbox and inbuilt validations")]
        public string ValidationGroup
        {
            get
            {
                EnsureChildControls();
                return validationGroup;

            }
            set
            {
                EnsureChildControls();
                validationGroup = value;

                requiredFieldValidator1.ValidationGroup = value;
                regularExpressionValidator1.ValidationGroup = value;
                maxlengthValidator.ValidationGroup = value;
                textBox1.ValidationGroup = value;
            }
        }
        #endregion

        #endregion

        #region -- blank properties--
        public string EmptyText { get; set; }
        public string CommaSeparatedData { set; get; }
        public bool ShowDefaultItem { get; set; }
        public string DropDownCssClass { get; set; }
        #endregion

        protected override void RecreateChildControls()
        {
            EnsureChildControls();
        }

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

            #region--textbox--
            textBox1 = new TextBox();
            textBox1.ID = "textbox1";
            //textBox1.CssClass = TextBoxClass;
            textBox1.Style[HtmlTextWriterStyle.VerticalAlign] = "middle";

            //if (!validationGroup.IsNullOrEmpty())
            //{
            //    textBox1.ValidationGroup = validationGroup;
            //}
            #endregion

            #region--required field validator--
            requiredFieldValidator1 = new RequiredFieldValidator();
            requiredFieldValidator1.ID = "validator1";
            requiredFieldValidator1.ErrorMessage = RequiredErrorMessage;
            requiredFieldValidator1.CssClass = validationClasses;
            requiredFieldValidator1.Display = ValidatorDisplay.Dynamic;
            requiredFieldValidator1.ControlToValidate = textBox1.ID;
            requiredFieldValidator1.Enabled = false;
            requiredFieldValidator1.SetFocusOnError = true;
            //if (isRequired)
            //{
            //    requiredFieldValidator1.Enabled = true;
            //}

            //if (!validationGroup.IsNullOrEmpty())
            //{
            //    requiredFieldValidator1.ValidationGroup = validationGroup;
            //}

            #endregion

            #region-- regular expression validator--
            regularExpressionValidator1 = new RegularExpressionValidator();
            regularExpressionValidator1.ID = "validator2";
            regularExpressionValidator1.Enabled = false;
            regularExpressionValidator1.ErrorMessage = RegularExpressionErrorMessage;
            regularExpressionValidator1.CssClass = validationClasses;
            regularExpressionValidator1.Display = ValidatorDisplay.Dynamic;
            regularExpressionValidator1.ControlToValidate = textBox1.ID;
            regularExpressionValidator1.SetFocusOnError = true;
            //if (!validationGroup.IsNullOrEmpty())
            //{
            //    regularExpressionValidator1.ValidationGroup = validationGroup;
            //}
            #endregion

            #region -- maxlength validation --
            maxlengthValidator = new RegularExpressionValidator();
            maxlengthValidator.ControlToValidate = textBox1.ID;
            maxlengthValidator.ID = "validator3";
            maxlengthValidator.Enabled = false;
            maxlengthValidator.CssClass = validationClasses;
            maxlengthValidator.Display = ValidatorDisplay.Dynamic;
            maxlengthValidator.SetFocusOnError = true;

            //if (!validationGroup.IsNullOrEmpty())
            //{
            //    maxlengthValidator.ValidationGroup = validationGroup;
            //}

            #endregion

            #region--integer Validation--
            integerValidator = new RegularExpressionValidator();
            integerValidator.ID = "integervalidator";
            integerValidator.Enabled = false;
            integerValidator.ErrorMessage = InvalidIntegerErrorMessage;
            integerValidator.CssClass = validationClasses;
            integerValidator.Display = ValidatorDisplay.Dynamic;
            integerValidator.ControlToValidate = textBox1.ID;
            integerValidator.SetFocusOnError = true;
            integerValidator.ValidationExpression=@"^\d+$";
            #endregion

            #region -- decimal validation--
            decimalValidator = new RegularExpressionValidator();
            decimalValidator.ID = "decimalvalidator";
            decimalValidator.Enabled = false;
            decimalValidator.ErrorMessage = InvalidDecimalErrorMessage;
            decimalValidator.CssClass = validationClasses;
            decimalValidator.Display = ValidatorDisplay.Dynamic;
            decimalValidator.ControlToValidate = textBox1.ID;
            decimalValidator.SetFocusOnError = true;
            decimalValidator.ValidationExpression = @"(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)";

            #endregion

            this.Controls.Add(textBox1);
            this.Controls.Add(requiredFieldValidator1);
            this.Controls.Add(regularExpressionValidator1);
            this.Controls.Add(maxlengthValidator);
            this.Controls.Add(integerValidator);
            this.Controls.Add(decimalValidator);
        }

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

        protected override void Render(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);

            textBox1.RenderControl(writer);
            requiredFieldValidator1.RenderControl(writer);
            regularExpressionValidator1.RenderControl(writer);
            maxlengthValidator.RenderControl(writer);
            integerValidator.RenderControl(writer);
            decimalValidator.RenderControl(writer);

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:06:00+00:00Added an answer on June 16, 2026 at 7:06 am

    I was unable to reproduce the problem. Composite controls do implement INamingContainer, but that doesn’t affect validation groups. Could you provide a small code example to help us troubleshoot?

    Here’s the code that worked for me:

    Composite control

    public class RequiredTextBox : CompositeControl
    {
        private TextBox _textBox = new TextBox { ID = "T" };
        private RequiredFieldValidator _validator =
            new RequiredFieldValidator { ID = "V", ControlToValidate = "T", ErrorMessage = "*" };
    
        public string Text
        {
            get { return _textBox.Text; }
            set { _textBox.Text = value; }
        }
    
        public string ValidationGroup
        {
            get { return _validator.ValidationGroup; }
            set { _validator.ValidationGroup = value; }
        }
    
        protected override void CreateChildControls()
        {
            this.Controls.Add(_textBox);
            this.Controls.Add(_validator);
        }
    }
    

    ASPX

    <form id="form1" runat="server">
        <asp:ValidationSummary runat="server" ID="ValidationSummary" /> 
        <ctl:RequiredTextBox runat="server" ID="TextBox" ValidationGroup="VG" />
        <asp:Button runat="server" ID="SubmitButton" Text="Submit" ValidationGroup="VG" />
    </form> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, this problem is a little complicated, so bear with me. I have a
Alright I did a little bit of research on this but I couldn't find
I'm having a little predicament switching between views, here. Alright, so, I have this
Alright this is my first day with JQuery so have fun with these functions
Alright I know this is more than likely a amateur question but I have
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, so, I'm doing a little script that downloads X number of wallpapers of
Alright this probably is the worst error I have found ever. I have two
Alright I'm a little lost. I've a page that pulls a value out of
Alright, here's the deal: I have a nice little 4Gb table called Mails on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.