Programming Microsoft ASP.NET 4 - Dino Esposito [120]
When Predictable is used, the markup you get for the Repeater shown earlier takes the following form:
- ALFKI
- ANATR
- BOTTM
If the Repeater control is being used within a master page, the ID of the content placeholder will prefix the ID—something like this:
MainContentPlaceholder_Repeater2_Element_0
The key difference between the two algorithms is all in the trailing token, which is now easy to guess and script and still guarantees uniqueness. The Predictable mode represents the default behavior you get for ASP.NET 4 applications. This is a potentially breaking change. If you have an ASP.NET 3.5 piece of code written to take advantage of the old-fashioned syntax of autogenerated IDs (mostly client script code), well, that code might fail after the application is recompiled to ASP.NET 4.
Important
Many of the posts and articles you can find list AutoID as the default setting for pages. This is not the case with the released version of ASP.NET 4, as you can read here: http://msdn.microsoft.com/en-us/library/950xf363(v=VS.100).aspx. You can also verify that on your own, going step by step through the Repeater example presented earlier.
The Predictable algorithm allows you some degree of further control over the generated ID, at least for controls that implement IDataKeysControl:
public interface IDataKeysControl
{
String[] ClientIDRowSuffix { get; }
DataKeyArray ClientIDRowSuffixDataKeys { get; }
}
In ASP.NET 4, only two controls natively implement this interface: GridView and ListView. Similar view controls, such as FormView and DetailsView controls, do not support the ClientIDRowSuffix property because they are not expected to display multiple rows.
Let’s consider a GridView control with a templated column:
With the default settings, the Predictable algorithm produces the following IDs for the elements via the Label control:
GridView1_Element_0
Try setting the ClientIDRowSuffix to a property name like the one shown here.
The GridView will emit the following markup: ALFKI ANATR BOTTM The property is an array of strings; if it’s set declaratively, you use a comma-separated string to list multiple properties whose values you want to retrieve in the ID. Note also that setting a parent to Static and then setting child elements to Predictable will start the naming container at the parent level, which is handy for always giving sections of pages unique IDs. Note The ClientIDRowSuffix property is not supported by the Repeater and DataList controls even though the control might output multiple rows. For any list controls, you have only the progressive number to distinguish between repeated templates. This was done essentially to discourage use of these controls, because they are considered deprecated in ASP.NET 4. ASP.NET Control Containers
Naming containers are not the only type