Finding RequiredFieldValidator Controls in ASP.NET Web Forms

Editorial illustration.

Originally published 22 June 2023; rewritten and reviewed on 15 October 2026.

If you still maintain enterprise portals or legacy Kentico sites built on ASP.NET Web Forms, you will eventually run into a form page stuffed with nested MasterPage placeholders, UpdatePanels, Repeaters, and .ascx user controls. One day a requirement lands on your desk: collect all RequiredFieldValidator IDs from the code-behind—maybe to highlight empty fields on the client, toggle a validation group conditionally, or log which mandatory inputs are active.

If you only loop through Page.Controls, you will get almost nothing back. At the top level, Page.Controls usually contains little more than a LiteralControl and the HtmlForm (or MasterPage). Every actual validator is buried several layers deep inside naming containers.

Walking the control tree recursively

To find every RequiredFieldValidator regardless of how deeply it is nested inside a Panel, MultiView, or user control, write a small recursive helper using yield return:

using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

private static IEnumerable<RequiredFieldValidator> FindRequiredValidators(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        if (child is RequiredFieldValidator validator)
        {
            yield return validator;
        }

        foreach (RequiredFieldValidator nested in FindRequiredValidators(child))
        {
            yield return nested;
        }
    }
}

You can call FindRequiredValidators(Page) during Page_Load, Page_PreRender, or inside a button click handler. Keep the Web Forms page lifecycle in mind, though: if your page adds user controls or Repeater items dynamically, make sure you run this traversal after those controls have been instantiated and added to the tree.

There is also a built-in shortcut worth knowing: Page.Validators. Web Forms maintains a ValidatorCollection on the Page object, so you can filter it with LINQ:

using System.Linq;

var validators = Page.Validators
    .OfType<RequiredFieldValidator>()
    .ToList();

Page.Validators is clean when you only care about validators that have already registered themselves with the page. However, recursive traversal is still useful when you need to scope your search to a specific container (for example, FindRequiredValidators(pnlShippingAddress)) rather than scanning the entire page.

Server ID vs rendered ClientID

Once you have the RequiredFieldValidator instances, pick the identifier that matches where you plan to use it:

  • validator.ID: The server-side ID you wrote in the .aspx or .ascx markup, such as rfvPostcode. Use this when calling FindControl inside the same NamingContainer on the server.
  • validator.UniqueID: The $-delimited name used in form postbacks, such as ctl00$MainContent$ucAddress$rfvPostcode.
  • validator.ClientID: The _-delimited DOM id rendered in HTML, such as MainContent_ucAddress_rfvPostcode (depending on ClientIDMode). Pass this to JavaScript if your script needs to inspect or style the validator element in the browser.

A common mistake is assuming validator.ID equals the HTML id in the browser. Unless ClientIDMode="Static" is set—which is dangerous inside Repeater or GridView rows because it produces duplicate DOM IDs—the browser will receive the prefixed ClientID.

If your goal is actually to highlight the input control that failed validation rather than the validator’s <span>, grab validator.ControlToValidate and resolve it against the validator’s own NamingContainer:

foreach (var validator in FindRequiredValidators(Page))
{
    if (!validator.Enabled || validator.IsValid)
    {
        continue;
    }

    Control target = validator.NamingContainer?.FindControl(validator.ControlToValidate);
    string targetClientId = target?.ClientID ?? string.Empty;
    // Pass targetClientId or validator.ClientID to your UI helper or script
}

Calling Page.FindControl(validator.ControlToValidate) directly would fail whenever the validator lives inside a user control or template row. Resolving from validator.NamingContainer keeps the lookup scoped to the right container.

Filter by validation group and validate on the server

Not every RequiredFieldValidator on a page belongs to the button the user just clicked. A checkout or profile page might have a search box in the header, a newsletter signup in the footer, and the main form in the middle. Before collecting or acting on validators, check validator.Enabled and validator.ValidationGroup so you do not accidentally flag controls from an unrelated section.

More importantly, finding validators does not execute them. Client-side validation (WebUIValidation.js) is only a convenience for the user; anyone can bypass it with browser DevTools or a crafted POST request. Always trigger Page.Validate(validationGroup) (or let the button’s CausesValidation="true" handle it) and check Page.IsValid on the server before saving data to SQL Server or calling an API.