Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# composite ASP.NET4.0
Hello everyone,
I am badly stuck in middle of project with this issue. Googled a lot but not getting a solution.
 
My objective is to create a composite control with a label, textbox, two required field validator and a custom validator. My custom control code is as follows:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AFGISServerControl
{
    [ToolboxData("<{0}:ValidatorTextBox runat="\""server\"> </{0}:ValidatorTextBox>")]
    public class ValidatorTextBox : CompositeControl
    {
        private TextBox InputTextBox;
        private Label InputLabel;
        private RequiredFieldValidator RequiredValidator1;
        private RequiredFieldValidator RequiredValidator2;
        private CustomValidator cv;
        private static readonly object EventValidateKey = new object();
 
        protected override void OnLoad(EventArgs e)
        {
            EnsureChildControls();
            base.OnLoad(e);
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            InputTextBox = new TextBox();
            InputLabel = new Label();
            InputTextBox.ID = "inputTextBox";
            //InputTextBox.AutoPostBack = true;
 
            RequiredValidator1 = new RequiredFieldValidator();
            RequiredValidator1.ID = this.ID + "_RFV1";
            RequiredValidator1.ControlToValidate = InputTextBox.ID;
            RequiredValidator1.ValidationGroup = string.Empty;
            RequiredValidator1.Display = ValidatorDisplay.Dynamic;
            RequiredValidator1.ErrorMessage = Caption.ToString() + " is mandatory";
 
            RequiredValidator2 = new RequiredFieldValidator();
            RequiredValidator2.ID = this.ID + "_RFV2";
            RequiredValidator2.ControlToValidate = InputTextBox.ID;
            RequiredValidator2.Display = ValidatorDisplay.Dynamic;
            RequiredValidator2.ErrorMessage = Caption.ToString() + " is mandatory";
 
            cv = new CustomValidator();
            cv.ID = this.ID + "_CV";
            cv.ControlToValidate = InputTextBox.ID;
            cv.SetFocusOnError = true;
            cv.Display = ValidatorDisplay.Dynamic;
            cv.ErrorMessage = this.CustomErrorMessage;
            cv.ValidateEmptyText = true;
            cv.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.cv_ServerValidate);
           
            this.Controls.Add(InputLabel);
            this.Controls.Add(InputTextBox);
            this.Controls.Add(RequiredValidator1);
            this.Controls.Add(RequiredValidator2);
            this.Controls.Add(cv);
        }
 
        #region properties
        [
        Bindable(true),
        Category("Default"),
        DefaultValue(""),
        Description("Value")
        ]
        public string Text
        {
            get
            {
                EnsureChildControls();
                return InputTextBox.Text;
            }
            set
            {
                EnsureChildControls();
                InputTextBox.Text = value;
            }
        }
        [
        Bindable(true),
        Category("Default"),
        DefaultValue(""),
        Description("Validation Group")
        ]
        public string ValidationGroup
        {
            get
            {
                EnsureChildControls();
                return InputTextBox.ValidationGroup;
            }
            set
            {
                EnsureChildControls();
                InputTextBox.ValidationGroup = value;
                RequiredValidator2.ValidationGroup = value;
                cv.ValidationGroup = value;
            }
        }
        [
        Bindable(true),
        Category("Default"),
        DefaultValue(""),
        Description("Error message for the custom validator.")
        ]
        public string CustomErrorMessage
        {
            get
            {
                EnsureChildControls();
                return cv.ErrorMessage;
            }
            set
            {
                EnsureChildControls();
                cv.ErrorMessage = value;
            }
        }
        [
        Bindable(true),
        Category("Default"),
        DefaultValue(""),
        Description("Validity of custom validator.")
        ]
        public Boolean isValid
        {
            get
            {
                EnsureChildControls();
                return cv.IsValid;
            }
            set
            {
                EnsureChildControls();
                cv.IsValid = value;
            }
        }
 
        [
        Bindable(true),
        Category("Default"),
        DefaultValue(""),
        Description("The text for the name label.")
        ]
        public string Caption
        {
            get
            {
                EnsureChildControls();
                return InputLabel.Text;
            }
            set
            {
                EnsureChildControls();
                InputLabel.Text = value;
            }
        }
        #endregion
 
        protected override void RecreateChildControls()
        {
            EnsureChildControls();
        }
 
        [
        Category("Action"),
        Description("Raised on Text Change")
        ]
        public event ServerValidateEventHandler Validate
        {
            add
            {
                Events.AddHandler(EventValidateKey, value);
            }
            remove
            {
                Events.RemoveHandler(EventValidateKey, value);
            }
        }
        protected virtual void OnValidate(ServerValidateEventArgs args)
        {
            EventHandler ValidateHandler = (EventHandler)Events[EventValidateKey];
            if (ValidateHandler != null)
            {
                ValidateHandler(this, args);
            }
        }
        protected void cv_ServerValidate(object source, ServerValidateEventArgs args)
        {
            OnValidate(args);
        }
 
        protected override void Render(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
            InputLabel.RenderControl(writer);
            InputTextBox.RenderControl(writer);
            RequiredValidator1.RenderControl(writer);
            RequiredValidator2.RenderControl(writer);
            cv.RenderControl(writer);
        }        
    }
}
 
My aspx code is
<cc1:ValidatorTextBox ID="ValidatorTextBox1"  runat="server" LabelText="Service No" CustomErrorMessage="Not Valid" ValidationGroup="test"  önValidate="CVText" />
<asp:Button ID="Button1" runat="server" Text="Button" />
 
The specific issue faced are
- Code behind method for "onvalidate" event is not being fired
- Required field validator error message is displaying only "is mandatory", the caption is not prefixed.
 
I will be greatful for the advice.
Posted 31-Oct-12 23:11pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

After breaking head, i changed the whole way of going about with this control. Went ahead with the useage of ivalidator. Thats quite an efficient way.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 337
1 DamithSL 310
2 OriginalGriff 220
3 Zoltán Zörgő 149
4 Peter Leow 115
0 OriginalGriff 7,510
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 5,044
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100