动态更改RangeValidator属性

时间:2013-08-22 20:40:18

标签: c# asp.net .net visual-studio-2010

如何根据所查看产品的具体类型对按钮单击时的RangeValidator1进行硬编码以执行不同的操作,例如:

if(id == 1)
{
RangeValidator1.Enabled = true;
RangeValidator1.Type = ValidationDataType.Integer;
RangeValidator1.MaximumValue = "6";
RangeValidator1.MinimumValue = "1";
RangeValidator1.ErrorMessage = "Only 6 allowed per customer";
}
else if(id == 2)
{
RangeValidator1.Enabled = true;
RangeValidator1.Type = ValidationDataType.Integer;
RangeValidator1.MaximumValue = "10";
RangeValidator1.MinimumValue = "1";
RangeValidator1.ErrorMessage = "Only 10 allowed per customer";
}
else
RangeValidator.Enabled = false;

这是按钮点击后我想要实现的一般要点,但是它引发了一个错误。“MaximumValue属性RangeValidator1的值”无法转换为Integer类型。

帮助?

1 个答案:

答案 0 :(得分:0)

RangeValidator1.MaximumValue需要像你一样的字符串值

RangeValidator1.MaximumValue = "10";

使其从整数变量中获取,将.ToString()添加到此变量中,如此

    int x = 10;
    RangeValidator1.MaximumValue = x.ToString();