如何在asp.net中使用范围验证器创建复合控件

时间:2017-01-22 13:08:24

标签: c# asp.net asp.net-customcontrol

我想创建一个带范围验证的复合自定义控件。可以是整数或日期。无法将MinimumValue和MaximumValue指定为属性。谁能帮我。我的代码可能不正确,但请建议我一个解决方案: - 我试图创建一个带范围验证的复合文本框控件。因为我已经声明了三个属性Bool IsRangeNum,Int MininumValue和int MaximumValue。 - 当IsRangeNum为' True'时,应分配MinimumValue和MaximumValue分配在文本框中输入的整数。 - 无法从.aspx页面检索MinimumValue和MaximumValue属性值 - ' int number = Convert.ToInt32(textBox1.Text);'文本框值正在转换为int。

[ToolboxData("<{0}:ValidationTextBox runat=server></{0}:ValidationTextBox>")]
public class ValidationTextBox : CompositeControl
{

 #region-- privatess --
 private TextBox textBox1;
 private RangeValidator RangeValidator1; 
 private bool isRange;
 private int minimumValue;
 private int maximumValue;
 #endregion 
 #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;
    }
 }
    #region  -- range validation --
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("Error message that appears when value in textbox is out of range ")]
    public String InvaildRangeErrorMessage
    {
        get
        {
            EnsureChildControls();
            return string.IsNullOrEmpty(RangeValidator1.ErrorMessage) ? "Invalid value" : RangeValidator1.ErrorMessage;
        }
        set
        {
            EnsureChildControls();
            RangeValidator1.ErrorMessage = value;
        }
    }

    [Bindable(true)]
    [Category("Default")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("Range Validation. Leave blank if you dont want to implement.")]
    public bool IsRangeNum
    {
    get
    {
        EnsureChildControls();
        return RangeValidator1.Enabled;

    }
    set
    {
        EnsureChildControls();
        RangeValidator1.Enabled = true;

        if (value == true)
        {
            int number = Convert.ToInt32(textBox1.Text);
            RangeValidator1.Enabled = true;

            if (number >= MinimumValue && number <= MaximumValue)
            {
                RangeValidator1.Enabled = true;
            }
            else
            {
                RangeValidator1.Enabled = false;
            }
        }
        else
        {
            RangeValidator1.Enabled = false; 
        }

    }
}
    [Bindable(true)]
[Category("Default")]
[DefaultValue("")]
[Localizable(true)]
[Description("Range Validation. Leave blank if you dont want to implement.")]
public int MinimumValue
{
    get
    {
        EnsureChildControls();
        return minimumValue;
    }
    set
    {
        EnsureChildControls();
        minimumValue = value;
        RangeValidator1.Enabled = true;
    }
}
[Bindable(true)]
[Category("Default")]
[DefaultValue("")]
[Localizable(true)]
[Description("Range Validation. Leave blank if you dont want to implement.")]
public int MaximumValue
{
    get
    {
        EnsureChildControls();
        return maximumValue;
    }
    set
    {
        EnsureChildControls();
        maximumValue = value;
        RangeValidator1.Enabled = true;
    }
}
        #endregion
        protected override void CreateChildControls()
        {
        Controls.Clear();
        #region--textbox--
        textBox1 = new TextBox();
        textBox1.ID = "textbox1";
        textBox1.Style[HtmlTextWriterStyle.VerticalAlign] = "middle";
        #endregion
        #region-- range validator--
        RangeValidator1 = new RangeValidator();
        RangeValidator1.ID = "validator8";
        RangeValidator1.Enabled = false;
        RangeValidator1.ErrorMessage = RegularExpressionErrorMessage;
        RangeValidator1.CssClass = validationClasses;
        RangeValidator1.Display = ValidatorDisplay.Dynamic;
        RangeValidator1.ControlToValidate = textBox1.ID;
        RangeValidator1.SetFocusOnError = true;
        #endregion
        this.Controls.Add(textBox1);
        this.Controls.Add(RangeValidator1);
        }
        protected override void OnLoad(EventArgs e)
        {
        EnsureChildControls();
        base.OnLoad(e);
        }
        protected override void Render(HtmlTextWriter writer)
        {
         AddAttributesToRender(writer);
         textBox1.RenderControl(writer);
         RangeValidator1.RenderControl(writer);
         RangeValidator2.RenderControl(writer);
        }

}

0 个答案:

没有答案