Elegant way of switching between ValidationSummary and single validation strings
In a nutshell, let's say I have two textboxes, both have RequiredFieldValidator
controls. I want to display only a single string in ValidationSummary
control if both of the values are not valid and one string, in the place of RequiredFieldValidator
if only one is not valid.
Currently, What I have is working but it is a bit messy. Was wondering if there is easier, more elegant way, of doing this.
if ((!Text1Valid.IsValid) && (!Text2Valid.IsValid))
{
// Make sure the individual validator shows nothing and ValidationSummary is visible.
Text1Valid.Display = ValidatorDisplay.None;
Text2Valid.Display = ValidatorDisplay.None;
ValidSummary.Visible = true;
Text1Valid.ErrorMessage = "Both of the values are wrong!";
Text2Valid.ErrorMessage = String.Empty;
}
else
{
// Single validation strings visible and ValidationSummary hidden.
Text1Valid.Display = ValidatorDisplay.Dynamic;
Text2Valid.Display = ValidatorDisplay.Dynamic;
ValidSummary.Visible = false;
Text1Valid.ErrorMessage = "The value is wron开发者_开发知识库g.";
Text2Valid.ErrorMessage = "The value is wrong.";
}
There definitely is not an out of box way to do what you want in a an easier/elegant fashion that I'm aware of. In terms of custom code it's a bit difficult to get a really elegant solution because the validation logic is very coupled. I can totally imagine the situation where you have a variety of validators on each input, differing numbers of "linked" inputs, and different types of input elements. If this is a one off situation, a solution similar to yours is probably ok. If this is a recurring theme for you project then you probably want to think about how much time you can afford to invest into a more robust solution.
精彩评论