Donn Felker

Not A Subscriber?

Join thousands of people who build and refuse to get left behind. One message a week on building, using AI as an accelerator, and staying sharp in the new AI frontier.

Receive one free message a week

July 20, 2007 Donn Felker

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.