简体   繁体   中英

Checkbox to control rangevalidator or maximum input double of textbox

I have a checkbox and the goal is to limit the maximum input value of a textbox when the checkbox is ticked.

I have two solutions in mind, one is to alter the value of the rangevalidator when the checkbox is ticked or the other is to restrict the value parsed into the textbox when the checkbox is ticked.

But my latter solution has an error as seen in the image below. While i don't know how to implement the former solution.

Code of the TextBox

<div class="d flex justify-content-start">
                    <div class="col-6 ">
                        Hours Spent
                            <asp:TextBox placeholder="Hours Spent *" Width="" ID="txtHoursSpent" runat="server" MaxLength="3"></asp:TextBox>
                        <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Value must be greater than zero."
                            ValidationExpression="\d+" ControlToValidate="txtHoursSpent" MinimumValue="0.5" MaximumValue="12" Type="Double"></asp:RangeValidator>

                    </div>
                </div>

Code Of the CheckBox

<asp:CheckBox Text="Half-Day" runat="server" OnCheckedChanged="HalfDay_CheckedChanged" />

As you can see the error in the codebehind causes me to be unable to restrict the textbox to a certain range. Code Behind of the Checkbox

Does anyone know the solution perhaps?

You are trying to compare a reference to the textbox itself (not even the value in the text box) to a number. This won't work because the textbox is a full, rich object.

You will have to get the value from the textbox, and then ensure that that value is itself a number (hint: double.TryParse ), and then compare THAT to the double.

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