Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a web form that populates data to textboxes. I have other textboxes that data can be entered to submit to the database. Along side of textboxex are RangeValidators.I have a code that should calculate to see if the value in textbox2 is higher or lower then value textbox1 by percentage.

Example:

If textbox1 is 100 and textbox2 is 200. Is that 20% higher or lower?

Textbox1 will be populated from the database and Textbox2 is the data that will be submitted to the database. What I am trying to do is to calculate to see if the new value in textbox2 is 20% higher or lower than the value in textbox1.

Here is the HTML Code I have:

ASP.NET
<table class="style2">
        <tr>
            <td class="style30">
                1. Total Number (headcount) Full-Time Undergraduate Students</td>
            <td class="style41">
                <asp:TextBox ID="TextBoxLYTNFUG1" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNFUG" runat="server" Width="180px" ></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNFUG1" runat="server" 
                    ControlToValidate="TextBoxTNFUG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" 
                    Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                2. Total Number (headcount) Full-Time Post-Baccalaureate Students</td>
            <td class="style41">
                <asp:TextBox ID="TextBoxLYTNFG2" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNFG" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNFG2" runat="server" 
                    ControlToValidate="TextBoxTNFG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" 
                    Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                3. Total Number (headcount) For-Credit, Part-Time Undergraduate Students</td>
            <td class="style41">
                <asp:TextBox ID="TextBoxLYTNCPUG3" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNCPUG" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNCPUG3" runat="server" 
                    ControlToValidate="TextBoxTNCPUG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" 
                    Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                4. Total Number (headcount) For-Credit, Part-Time Post-Baccalaureate Students</td>
            <td class="style41">
                <asp:TextBox ID="TextBoxLYTNCPG4" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNCPG" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNCPG4" runat="server" 
                    ControlToValidate="TextBoxTNCPG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" 
                    Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                5. Total Number (headcount) students enrolled in Non-Credit courses</td>
            <td class="style41">
                <asp:TextBox ID="TextBoxLYTNNCC5" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNNCC" runat="server" Width="180px" AutoPostBack="True" 
                    ontextchanged="TextBoxTNNCC_TextChanged" ></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNNCC5" runat="server" 
                    ControlToValidate="TextBoxTNNCC" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" 
                    Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                6. Total Headcount of All Students</td>
            <td class="style41">
                 </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTHCAS" runat="server" Width="180px" Enabled="False" 
                    ReadOnly="True"></asp:TextBox>
            </td>
            <td>
                 </td>
        </tr>
    </table>


Here is my code behind:

C#
protected void InitRangeValidations()
    {
        int itxtVal1 = Convert.ToInt32(TextBoxLYTNFUG1.Text);
        int iMinVal1 = (itxtVal1 - itxtVal1 * 20 / 100);
        int iMaxVal1 = (itxtVal1 + itxtVal1 * 20 / 100);
        RangeValidatorLYTNFUG1.MinimumValue = iMinVal1.ToString();
        RangeValidatorLYTNFUG1.MaximumValue = iMaxVal1.ToString();

        int itxtVal2 = Convert.ToInt32(TextBoxLYTNFG2.Text);
        int iMinVal2 = (itxtVal2 - itxtVal2 * 20 / 100);
        int iMaxVal2 = (itxtVal2 + itxtVal2 * 20 / 100);
        RangeValidatorLYTNFG2.MinimumValue = iMinVal2.ToString();
        RangeValidatorLYTNFG2.MaximumValue = iMaxVal2.ToString();

        int itxtVal3 = Convert.ToInt32(TextBoxLYTNCPUG3.Text);
        int iMinVal3 = (itxtVal3 - itxtVal3 * 20 / 100);
        int iMaxVal3 = (itxtVal3 + itxtVal3 * 20 / 100);
        RangeValidatorLYTNCPUG3.MinimumValue = iMinVal3.ToString();
        RangeValidatorLYTNCPUG3.MaximumValue = iMaxVal3.ToString();

        int itxtVal4 = Convert.ToInt32(TextBoxLYTNCPG4.Text);
        int iMinVal4 = (itxtVal4 - itxtVal4 * 20 / 100);
        int iMaxVal4 = (itxtVal4 + itxtVal4 * 20 / 100);
        RangeValidatorLYTNCPG4.MinimumValue = iMinVal4.ToString();
        RangeValidatorLYTNCPG4.MaximumValue = iMaxVal4.ToString();

        int itxtVal5 = Convert.ToInt32(TextBoxLYTNNCC5.Text);
        int iMinVal5 = (itxtVal5 - itxtVal5 * 20 / 100);
        int iMaxVal5 = (itxtVal5 + itxtVal5 * 20 / 100);
        RangeValidatorLYTNNCC5.MinimumValue = iMinVal5.ToString();
        RangeValidatorLYTNNCC5.MaximumValue = iMaxVal5.ToString();
    }


Will this solution give me the calculation to see if the value in textbox2 is higher or lower than the value in textbox1 by 20%?
Posted
Updated 4-Sep-14 3:59am
v2
Comments
ZurdoDev 3-Sep-14 16:04pm    
What's your question?
Computer Wiz99 3-Sep-14 19:57pm    
Is my calculation correct for getting the percentage? I understand the order of operations. Trying to make sure the my code is doing it right.
ZurdoDev 3-Sep-14 20:38pm    
Just test your code to see if it is working.
PhilLenoir 3-Sep-14 16:14pm    
The naming is a little confusing, especially as we don't see your XHTML (which control each validator is bound to). In your question to use "Textbox1" and "Textbox2". Your actual textbox controls are called "TextBoxLYTNFUGn" and your validators are named "RangeValidatorLYTNFUGn". If n is 1 and RangeValidatorLYTNFUG1 is bound to TextBoxLYTNFUG1 then I think you have something wrong (You are binding to the control you are deriving the range from - is the user going to edit these values?).

Have you tried your code? If yes, how is it behaving that doesn't meet expectations?

Please use the improve question function to add some detail.

1 solution

1. If data in the first TextBox is coming from the database, you only need a rangevalidator on textboxes 2-5.
2. The rangevalidator for the second textbox should use the data from the first textbox and so on.
C#
protected void InitRangeValidations()
    {
        // No validation needed on the first textbox
        // int itxtVal1 = Convert.ToInt32(TextBoxLYTNFUG1.Text);
        // int iMinVal1 = (itxtVal1 - itxtVal1 * 20 / 100);
        // int iMaxVal1 = (itxtVal1 + itxtVal1 * 20 / 100);
        // RangeValidatorLYTNFUG1.MinimumValue = iMinVal1.ToString();
        // RangeValidatorLYTNFUG1.MaximumValue = iMaxVal1.ToString();

        int itxtVal1 = Convert.ToInt32(TextBoxLYTNFG1.Text); // For the second validator, look at the value from the FIRST textbox
        int iMinVal2 = (itxtVal1 - itxtVal1 * 20 / 100); // Calculate min value for SECOND textbox based on value from the FIRST textbox
        int iMaxVal2 = (itxtVal1 + itxtVal1 * 20 / 100); // Calculate max value for SECOND textbox based on value from the FIRST textbox
        RangeValidatorLYTNFG2.MinimumValue = iMinVal2.ToString();
        RangeValidatorLYTNFG2.MaximumValue = iMaxVal2.ToString();

        int itxtVal2 = Convert.ToInt32(TextBoxLYTNCPUG2.Text);
        int iMinVal3 = (itxtVal2 - itxtVal2 * 20 / 100);
        int iMaxVal3 = (itxtVal2 + itxtVal2 * 20 / 100);
        RangeValidatorLYTNCPUG3.MinimumValue = iMinVal3.ToString();
        RangeValidatorLYTNCPUG3.MaximumValue = iMaxVal3.ToString();

        int itxtVal4 = Convert.ToInt32(TextBoxLYTNCPG3.Text);
        int iMinVal4 = (itxtVal3 - itxtVal3 * 20 / 100);
        int iMaxVal4 = (itxtVal3 + itxtVal3 * 20 / 100);
        RangeValidatorLYTNCPG4.MinimumValue = iMinVal4.ToString();
        RangeValidatorLYTNCPG4.MaximumValue = iMaxVal4.ToString();

        int itxtVal5 = Convert.ToInt32(TextBoxLYTNNCC4.Text);
        int iMinVal5 = (itxtVal4 - itxtVal4 * 20 / 100);
        int iMaxVal5 = (itxtVal4 + itxtVal4 * 20 / 100);
        RangeValidatorLYTNNCC5.MinimumValue = iMinVal5.ToString();
        RangeValidatorLYTNNCC5.MaximumValue = iMaxVal5.ToString();
    }

Looking at this highly repetitive code, I would have introduced a method that contains this logic only once:
C#
protected void InitRangeValidations()
{
    CalculateAndSetRange(TextboxLYTNFG1.Text, RangeValidatorLYTNFG2);
    CalculateAndSetRange(TextboxLYTNCPUG2.Text, RangeValidatorLYTNCPUG3);
    CalculateAndSetRange(TextboxLYTNCPG3.Text, RangeValidatorLYTNCPG4);
    CalculateAndSetRange(TextboxLYTNNCC4.Text, RangeValidatorLYTNNCC5);
}

private void CalculateAndSetRange(string referenceValue, RangeValidator validator)
{
   int minVal = (referenceValue - referenceValue * 20 / 100);
   int maxVal = (referenceValue + referenceValue * 20 / 100);
   validator.MinimumValue = minVal.ToString();
   validator.MaximumValue = maxVal.ToString();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900