复合控件中的自定义验证器未触发

时间:2012-11-01 18:18:20

标签: asp.net composite-controls

我在这个问题上陷入了困境。谷歌搜索了很多但没有得到解决方案。

我的目标是创建一个带有标签,文本框,两个必填字段验证器和一个自定义验证器的复合控件。我的自定义控制代码如下: -

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);
        }        
    }
}

我的aspx代码是

    <cc1:ValidatorTextBox ID="ValidatorTextBox1" runat="server" Caption="Service No"
        CustomErrorMessage="Not Valid" ValidationGroup="test" 
        OnValidate="CVText" />
    <asp:Button ID="Button1" runat="server" Text="Button" />

aspx.cs

面临的具体问题是

  • &#34; onvalidate&#34;的代码隐藏方法事件未被解雇
  • 必填字段验证器错误消息仅显示&#34;是强制性的&#34;,标题不带前缀。

我会很乐意接受这个建议。

2 个答案:

答案 0 :(得分:0)

一些事情:

  • 您需要在源控件上调用Page.Validate()或使用CausesValidation = true触发回发。确保在执行验证时未指定ValidationGroup或使用“test”ValidationGroup。
  • 在CreateChildControls期间正在设置必填字段验证程序。您的标题是从输入文本中提取的,直到初始化后才会填充。在加载ViewState之前,您的CreateChildControls可能正在发生。您可能希望使用JavaScript来设置RequiredFieldValidator文本,或者使用_TextChanged事件处理程序在文本更改时更新错误消息。您采取的方法显然取决于您的具体用例要求。

答案 1 :(得分:0)

打破头后,我用这个控制改变了整个行进方式。继续使用ivalidator。这是一种非常有效的方式。