如果我想从*.aspx静态创建RangeValidator,它会是这样的:
<asp:RangeValidator Type="Double" MinimumValue="0.00" MaximumValue="100.00"
ID="rangeValidatorSomething" ControlToValidate="textBoxSomething" runat="server"
Display="Dynamic" >
<img src="../images/error.gif" alt="Validation error" title="Validation error" />
This number should be between 0.00 and 100.00
</asp:RangeValidator>
我想以编程方式创建相同的RangeValidator控件,因为我想允许用户选择他/她想要提供的数据量(即TextBox控件的数量)。因此,我需要确保新创建的TextBox控件中的数据是有效的。这就是为什么我想从下面的代码中创建一个RangeValidator:
RangeValidator dynamicRangeValidator = new RangeValidator();
dynamicRangeValidator.MinimumValue = "0.00";
dynamicRangeValidator.MaximumValue = "100.00";
dynamicRangeValidator.Type = ValidationDataType.Double;
...
我的问题是:我应该如何实现RangeValidator控件内的图像和文本,如上面的*.aspx示例所示?
有关所需字段验证器,请参阅以下示例:
<asp:TextBox runat="server" ID="textValue"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" Text='<img src="error.png" />' ControlToValidate="textValue"></asp:RequiredFieldValidator>
您可以使用控件添加方法。如以下
HtmlImage hi = new HtmlImage();
// set other properties and attributes of image
RangeValidator dynamicRangeValidator = new RangeValidator();
dynamicRangeValidator.Controls.Add(hi);