Not a subscriber?

Join thousands of others who are building self-directed lives through creativity, grit, and digital strategy—breaking free from the 9–5.
Receive one free message a week

HOW TO: Display Required Field Validators at Page Load

Sometimes when a page loads you’ll want the user to be able to see the which fields are required. This is normally done by presenting an asterisk next to the fields that need to be populated/selected.

The RequiredFieldValidator does not automatically perform validation upon load, but here’s some code that will allow you to do so:

I usually put this in the OnPreRender event handler as at that point everything is done processing on the page.

foreach (IValidator validator in Validators)
{
    if (validator is RequiredFieldValidator)
   {
     validator.Validate();
   }
}

This will ensure that the page’s required field validators are validated when the page loads. Therefore giving the user the insight into the required fields.