CustomControl中的RequiredFieldValidator [英] RequiredFieldValidator in CustomControl

查看:62
本文介绍了CustomControl中的RequiredFieldValidator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在通过扩展TextBox准备控件.在这里,我包括RequiredFieldValidator和范围验证器.但是这些都不起作用.您能帮我吗?下面是CustomControl的代码.

Hello,

I am preparing a control by extending TextBox. Here i included RequiredFieldValidator and range validators. But these are not working. Can you help me?Below is the code for CustomControl.

public class PasswordTextBox : TextBox
    {
        RequiredFieldValidator rfv = new RequiredFieldValidator();
        RangeValidator rv = new RangeValidator();

        private string minlength;
       public string Minlength
       {
           get { return minlength; }
           set { minlength = value; }
       }
       private string maxlength;
       public string Maxlength
       {
           get { return maxlength; }
           set { maxlength = value; }
       }

       protected override void OnInit(EventArgs e)
       {
           this.TextMode = TextBoxMode.Password;
           rfv.ErrorMessage = "*";
           rfv.ControlToValidate = this.ClientID;
           rv.ErrorMessage = "Password is not in range";
           rv.ControlToValidate = this.ClientID;
           rv.MinimumValue = minlength.ToString();
           rv.MaximumValue = maxlength.ToString();
       }
       protected override void RenderContents(HtmlTextWriter output)
       {
           this.RenderControl(output);
           rfv.RenderControl(output);
           rv.RenderControl(output);
       }
}

推荐答案

您可能还希望控件也继承IValidator接口. IValidator包含以下属性:
You may want your control to inherit the IValidator interface as well. The IValidator contains the following properties :
public string ErrorMessage
public bool IsValid


以及方法


and the method

public void Validate()


所以您的类声明为:


So your class declaration would be :

public class PasswordTextBox : TextBox, IValidator


祝你好运!
爱德华


Good luck!
Eduard


这篇关于CustomControl中的RequiredFieldValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆