RangeValidator代码的问题 [英] An Issue with the RangeValidator Code

查看:110
本文介绍了RangeValidator代码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10个文本框的表单。从数据库中自动填充5个文本框。另外5个是供用户输入数字。我还有5个rangevalidators。当我测试代码时,我收到一个错误。 输入字符串的格式不正确.RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(100 - Convert.ToInt32(TextBoxLYTNFUG.Text)* 20/100);我做错了什么?有人可以帮忙吗?



I have a form that has 10 textboxes on it. 5 textboxes are auto populated from the database. the other 5 is for the user to enter numbers. I also have 5 rangevalidators on it. When I test the code I get an error. "Input string was not in a correct format. RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(100 - Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);" What did I do wrong? Can someone please help?

protected void Page_Load(object sender, EventArgs e)
    {
        ButtonPrint.Attributes.Add("onclick", "window.print(); return false");

        RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) - Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);
        RangeValidatorLYTNFUG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) + Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);

        RangeValidatorLYTNFG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFG.Text) - Convert.ToInt32(TextBoxLYTNFG.Text) * 20 / 100);
        RangeValidatorLYTNFG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFG.Text) + Convert.ToInt32(TextBoxLYTNFG.Text) * 20 / 100);

        RangeValidatorLYTNCPUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPUG.Text) - Convert.ToInt32(TextBoxLYTNCPUG.Text) * 20 / 100);
        RangeValidatorLYTNCPUG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPUG.Text) + Convert.ToInt32(TextBoxLYTNCPUG.Text) * 20 / 100);

        RangeValidatorLYTNCPG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPG.Text) - Convert.ToInt32(TextBoxLYTNCPG.Text) * 20 / 100);
        RangeValidatorLYTNCPG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPG.Text) + Convert.ToInt32(TextBoxLYTNCPG.Text) * 20 / 100);

        RangeValidatorLYTNNCC.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNNCC.Text) - Convert.ToInt32(TextBoxLYTNNCC.Text) * 20 / 100);
        RangeValidatorLYTNNCC.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNNCC.Text) + Convert.ToInt32(TextBoxLYTNNCC.Text) * 20 / 100);
        TextBoxFTUG.Focus();







<td>
                <asp:RangeValidator ID="RangeValidatorLYTNFUG" 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="style26">
                <asp:TextBox ID="TextBoxLYTNFG" 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="RangeValidatorLYTNFG" 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="style26">
                <asp:TextBox ID="TextBoxLYTNCPUG" 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="RangeValidatorLYTNCPUG" 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="style26">
                <asp:TextBox ID="TextBoxLYTNCPG" 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="RangeValidatorLYTNCPG" 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="style26">
                <asp:TextBox ID="TextBoxLYTNNCC" 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="RangeValidatorLYTNNCC" 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="style26">
                 </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTHCAS" runat="server" Width="180px" Enabled="False" 

                    ReadOnly="True"></asp:TextBox>
            </td>
            <td>
                 </td>
        </tr>
    </table>
    <br />

推荐答案

You need to post your ASP page, but I’m guessing you didn’t properly identify the type of validation you want to do, here is an example (from MSDN):



You need to post your ASP page, but I'm guessing you didn't properly identify the type of validation you want to do, here is an example (from MSDN):

<asp:rangevalidator id="Range1" xmlns:asp="#unknown">
           ControlToValidate="TextBox1"
           MinimumValue="1"
           MaximumValue="10"
           Type="Integer"
           EnableClientScript="false"
           Text="The value must be from 1 to 10!"
           runat="server"/>
</asp:rangevalidator>





Note the \"Type\" parameter which is set to \"Integer\", if you have that set incorrectly, you will get an exception.



Full MSDN Example[^]



[Edit]

To see what part is failing, try this as the first operation in your method:





Note the "Type" parameter which is set to "Integer", if you have that set incorrectly, you will get an exception.

Full MSDN Example[^]


To see what part is failing, try this as the first operation in your method:

int textInt1 = Convert.ToInt32(TextBoxLYTNFUG.Text);
int textInt2 = Convert.ToInt32(TextBoxLYTNFUG.Text);

RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(textInt1 - textInt2 * 20 / 100);





But its apparent from the above that the MinimumValue will always be zero (you are subtracting the same value from itself, so that equals zero, zero times anything is zero, and zero divided by 10 is zero).



Anyway, try the above and I think you may find that your text in the textbox isn’t being converted properly.



[Edit 2]



Here is what you should do to calculate your min/max values:





But its apparent from the above that the MinimumValue will always be zero (you are subtracting the same value from itself, so that equals zero, zero times anything is zero, and zero divided by 10 is zero).

Anyway, try the above and I think you may find that your text in the textbox isn't being converted properly.



Here is what you should do to calculate your min/max values:

int value = ;

RangeValidatorLYTNFUG.MinimumValue = ((int)(Convert.ToInt32(TextBoxLYTNFUG.Text) * 0.8f)).ToString();
RangeValidatorLYTNFUG.MaximumValue = ((int)(Convert.ToInt32(TextBoxLYTNFUG.Text) * 1.2F)).ToString();





Which is much cleaner than what you wrote :)



Which is much cleaner than what you wrote :)


这篇关于RangeValidator代码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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