how to create a composite control with range validator in asp.net

133 Views Asked by At

I want to create a composite custom control with range validation. that may be integer or date. Unable to assign MinimumValue and MaximumValue as the properties. Can anyone help me. My code may not be correct but please suggest me a solution: - Am trying to create a composite textbox control with range validation. for that i have declared three properties Bool IsRangeNum, Int MininumValue and int MaximumValue. - When IsRangeNum is 'True', MinimumValue and MaximumValue are assigned integer entered in textbox should be validated. - Unable to retrieve MinimumValue and MaximumValue property value from .aspx page - 'int number = Convert.ToInt32(textBox1.Text);' textbox value is getting converted to 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

There are 0 best solutions below