C# 从DropDownList继承并添加验证器,可能吗?

C# 从DropDownList继承并添加验证器,可能吗?,c#,asp.net,custom-controls,C#,Asp.net,Custom Controls,我已经创建了从它继承的自定义文本框控件,一切都很好。但是,我在尝试使用DropDownList时遇到了一个问题。我在Google上搜索了大约2个小时,得到的结果要么是蹩脚的链接,要么是关于创建复合控件并在其中添加DropDownList的一些不完整的建议,但这也意味着我必须公开我使用的所有事件和属性,我觉得这对于我需要做的事情来说太过分了,在我的DropDownList控件旁边添加任何类型的验证器 举例来说,这就是我试图做的: using System; using System.Collect

我已经创建了从它继承的自定义文本框控件,一切都很好。但是,我在尝试使用DropDownList时遇到了一个问题。我在Google上搜索了大约2个小时,得到的结果要么是蹩脚的链接,要么是关于创建复合控件并在其中添加DropDownList的一些不完整的建议,但这也意味着我必须公开我使用的所有事件和属性,我觉得这对于我需要做的事情来说太过分了,在我的DropDownList控件旁边添加任何类型的验证器

举例来说,这就是我试图做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace Blah
{
    public class ExtendedDropDownList : DropDownList
    {

        private DropDownList _self;
        private CustomValidator _cv;
        public bool Required { get; set; }
        public String FieldName { get; set; }

        protected override void OnInit(EventArgs e)
        {
            _cv = new CustomValidator()
            {
                ControlToValidate = ID,
                EnableClientScript = false,
                ValidationGroup = ValidationGroup,
                ValidateEmptyText = true
            };
            _cv.ServerValidate += new ServerValidateEventHandler(_cv_ServerValidate);
            Controls.Add(_cv);
        }

        private void _cv_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (Required && String.IsNullOrWhiteSpace(args.Value))
            {
                args.IsValid = false;
                _cv.ErrorMessage = "The field <strong>" + FieldName + "</strong> is required.";
                return;
            }            
        }

    }
}
它抛出了一个异常,DropDownList不能有辅助控件。怎么会?如果文本框允许

有没有办法在不创建复合控件和重写控制盘的情况下执行相同的操作?是的,双关语意图:P。我假设我可以创建控件,然后在DropDownList渲染后在渲染阶段编写它,但我不知道如何做,如果可能的话,即使是一次黑客攻击,我需要完成的表单生成器的时间很短,这花费了太长时间,让我感到非常累:。。。你们知道,当我用尽所有可用的资源时,我就会这么做


提前谢谢!:D

好的,我按照我想要的方式做了,但是在容器页面的帮助下:

我必须将此添加到我的aspx页面:

<asp:PlaceHolder runat="server" ID="phValidators" />
然后是完成的控件,它将验证器添加到我创建的页面的验证器占位符中,而不是控件本身:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace Blah
{
    public class ExtendedDropDownList : DropDownList
    {
        private CustomValidator _cv;
        public bool Required { get; set; }
        public String FieldName { get; set; }
        public String ValidatorPlaceHolder { get; set; }

        protected override void OnInit(EventArgs e)
        {
            _cv = new CustomValidator()
            {
                ControlToValidate = ID,
                EnableClientScript = false,
                ValidationGroup = ValidationGroup,
                Display = ValidatorDisplay.None,
                ValidateEmptyText = true
            };
            _cv.ServerValidate += new ServerValidateEventHandler(_cv_ServerValidate);
            if (Parent.FindControl(ValidatorPlaceHolder) != null)
                Parent.FindControl(ValidatorPlaceHolder).Controls.Add(_cv);
            else
                throw new Exception("Cannot find asp:PlaceHolder inside parent with ID '" + ValidatorPlaceHolder + "'");
            base.OnInit(e);
        }

        private void _cv_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (Required && String.IsNullOrWhiteSpace(args.Value))
            {
                args.IsValid = false;
                _cv.ErrorMessage = "The field <strong>" + FieldName + "</strong> is required.";
                return;
            }            
        }

    }
}

我希望这能帮助一些人:

你在下面的C上做过谷歌搜索吗?在DropDownList中添加验证器会产生大量的结果是的,我做过,但它只谈到在HTML代码中添加验证器。我需要将其作为服务器控件,而不是用户控件。