范围验证器可以获得20% [英] Range Validator that can get 20%

查看:54
本文介绍了范围验证器可以获得20%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将数据填充到文本框的Web表单。我有其他文本框可以输入数据以提交到数据库。在textboxex的旁边是RangeValidators。我有一个代码应该计算,以查看textbox2中的值是高还是低于textbox1的百分比值。



示例:



如果textbox1是100而textbox2是200.那是20%还是更低?



Textbox1将从数据库填充,Textbox2是将提交给数据库的数据。我想要做的是计算以查看textbox2中的新值是否比textbox1中的值高20%或更低。



这是HTML代码我有:



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:

<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:





Here is my code behind:

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%?



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%?

推荐答案

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.

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.
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:


Looking at this highly repetitive code, I would have introduced a method that contains this logic only once:

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();
}


这篇关于范围验证器可以获得20%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆