Improving ASP.NET: Finding controls
This is part of a series of posts offering ideas to improve ASP.NET.
I’m not a big fan of the FindControl() method (found on System.Web.UI.Control) for these reasons:
- To prepare for the search, it needs to build a sorted collection of all controls found in the NamingContainer for which its invoked. Fortunately, this happens once per NamingContainer. Unfortunately, this work has to occur with each page request.
- It looks through the list of all available controls in the naming container. Many controls contain their own child controls which are never assigned IDs by the user. So why would you want to search through those child controls? There should be a way to narrow the search.
- And the usual: it only searches within one NamingContainer (except when you use the $ syntax, but that’s a special case.)
You may not think you are using FindControl, but each web control that has a property taking an ID, like RangeValidator.ControlToValidate, uses it to retrieve the actual control instance. In my opinion, the user is better served by avoiding FindControl and attaching the control instance directly. I do this throughout the controls of Peter’s Data Entry Suite. Let’s look at how my validator gets the ID to your textbox.
public class BaseValidator : WebControl
{
public string ControlIDToEvaluate { get; set; }
public Control ControlToEvaluate { get; set; }
}
This allows two ways to attach the TextBox to the validator. Users would work with the ASP.NET markup must use ControlIDToEvaluate (and FindControl), but those who are willing to write code can avoid FindControl.
<asp:TextBox id="TextBox1" runat="server" />
<des:RangeValidator id="RangeValidator1" runat="server" />
RangeValidator1.ControlToEvaluate = TextBox1;
Keep in mind that ASP.NET already has references to controls found on the page (in this case, RangeValidator1 and TextBox1). The difference is significant in terms of how much CPU time is spent. One assignment as opposed to a search that must prepare a sorted collection before it runs.
I would like to see ASP.NET web controls to offer the same capabilities.