RangeValidator加载页面时出错 [英] RangeValidator Error when loading page

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

问题描述

我有一个表格,上面有5个rangevalidators。页面加载时出现此错误:



I have a form that has 5 rangevalidators on it. When the page loads this error comes up:

Input string was not in a correct format.
Line 82:         RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) - Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);





所以我确保文本框填充的数据库中包含数据。我为所有5个范围验证器设置了这些代码:





So I made sure that the database the textboxes was populating from had data in it which it does. I have these codes in place for all 5 rangevalidators:

protected void Page_Load(object sender, EventArgs e)
{
 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);
}





代码有效但我还添加了另一个文本框填充代码来填充数据表单上的其他文本框用户刚刚提交的内容。在用户提交之前,该数据库中没有任何内容。我错误地得到这个错误?



这是TextBox_TextChange代码:





The code works but I also add another textbox populated code to populate the other textboxes on the form of the data that the user just submitted. There is nothing in that database yet until the user submits it. What am I doing wrong to get this error?

Here is the TextBox_TextChange Code:

protected void TextBoxTNNCC_TextChanged(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBoxTNFUG.Text);
        int b = Convert.ToInt32(TextBoxTNFG.Text);
        int c = Convert.ToInt32(TextBoxTNCPUG.Text);
        int d = Convert.ToInt32(TextBoxTNCPG.Text);
        int f = Convert.ToInt32(TextBoxTNNCC.Text);
        TextBoxTHCAS.Text = Convert.ToString(a + b + c + d + f);

    }

推荐答案

由于转换而发生此错误。该值为非数字或空白字符串。

使用答案检查此类似问题以解决您的问题。

从字符串转换为int [ ^ ]



几点建议:

不要在 Page_Load()中放入大量代码,为该&创建一个函数/子过程。在 Page_Load()中调用它,如下所示。

This error occurs because of conversion. The value is non-numeric or blank string.
Check this similar question with answer to solve your issue.
Conversion from string to int[^]

Few suggestions:
Don't put massive code in Page_Load(), create a function/sub procedure for that & call it in Page_Load() like below.
protected void Page_Load(object sender, EventArgs e)
{
    InitRangeValidations();
}
protected void InitRangeValidations()
{
 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);

/*
Remaining massive code blah blah blah
*/
}



并尝试简化代码,下面的代码比你的简单,同意吗?它更具可读性。我可以简化甚至更多(比如减少2或3行)。


And try to simplify your code, the below one is simpler than yours, agree? It's more readable one. I could simplify even more than this(like reducing 2 or 3 lines).

protected void InitRangeValidations()
{
int itxtVal = Convert.ToInt32(TextBoxLYTNFUG.Text);//Modify this line based on above link as I'm giving you as exercise for you
int iMinVal = (itxtVal - itxtVal * 20 / 100);
int iMaxVal = (itxtVal + itxtVal * 20 / 100);
RangeValidatorLYTNFUG.MinimumValue = iMinVal.ToString();
RangeValidatorLYTNFUG.MaximumValue = iMaxVal.ToString();
}



我知道你在这里发布问题并定期提供明确的细节,继续这样做。这里建议您优化代码。

编写更好的C#/ .NET代码的一些实践 [ ^ ]

C#应用程序开发的一些最佳实践(解释) [ ^ ]


这篇关于RangeValidator加载页面时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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