简体   繁体   English

尝试在ASP.NET中以编程方式设置RangeValidator的最大值和最小值

[英]Trying to set Max and Min value of RangeValidator programatically in ASP.NET

I am trying to set Max and Min value of RangeValidator programatically as below: 我试图以编程方式如下设置RangeValidator的最大值和最小值:

<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RangeValidator" 
MaximumValue="<%# Max %>" MinimumValue="<%# Min %>" Type="Integer" > 
Value should be in <%# Min %> and <%# Max %> </asp:RangeValidator>

Here, Min and Max are integer from code behind. 在这里,Min和Max是后面代码的整数。 However getting error: "The value '' of the MaximumValue property of 'RangeValidator1' cannot be converted to type 'Integer'." 但是,出现错误:“ RangeValidator1”的MaximumValue属性的“值”无法转换为类型“整数”。” I will really appreciate any help in this regard. 在这方面的任何帮助,我将非常感谢。 Thanks 谢谢

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
 <asp:RangeValidator ID="RangeValidator1" runat="server"ErrorMessage="RangeValidator"></asp:RangeValidator>
    </form>
</body>
</html>

Code For Codebehind 背后的代码

 protected void Page_Load(object sender, EventArgs e)
        {
            RangeValidator1.MinimumValue = 1.ToString();
            RangeValidator1.MaximumValue = 5.ToString();
        }

above code helps you to make it working. 上面的代码可帮助您使其正常工作。

Following is complete syntax for RangeValidator. 以下是RangeValidator的完整语法。

<asp:RangeValidator 
         id="ProgrammaticID" 
         ControlToValidate="ProgrammaticID of control to validate" 
         MinimumValue="value"
         MaximumValue="value" 
         Type="DataType" 
         ErrorMessage="Message to display in ValidationSummary control"
         Text="Message to display in control"
         ForeColor="value" 
         BackColor="value"  
         runat="server" >
    </asp:RangeValidator>

Note The RangeValidator control throws an exception if the value specified by the MaximumValue or MinimumValue property cannot be converted to the data type specified by the Type property. 注意如果不能将MaximumValue或MinimumValue属性指定的值转换为Type属性指定的数据类型,则RangeValidator控件将引发异常。 MSDN Source . MSDN来源

So try setting Type attribute properly or change the Max and Min Value to appropriate type. 因此,请尝试正确设置“ 类型”属性,或将“ 最大值最小值”更改为适当的类型。 In your case try converting Min and Max to Integer. 在您的情况下,请尝试将MinMax转换为Integer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM