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()
{
int itxtVal1 = Convert.ToInt32(TextBoxLYTNFG1.Text);
int iMinVal2 = (itxtVal1 - itxtVal1 * 20 / 100);
int iMaxVal2 = (itxtVal1 + itxtVal1 * 20 / 100);
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:
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();
}