简体   繁体   中英

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:

<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. However getting error: "The value '' of the MaximumValue property of 'RangeValidator1' cannot be converted to type 'Integer'." 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.

<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. MSDN Source .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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