我想创建一个具有范围验证的复合自定义控件。可能是整数或日期。无法将 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);
}
}