The control abstracts
this process by using a CreateControlHierarchy helper method to do the child creation work.
CreateControlHierarchy contains code to add the templates and the hyperlinks as child controls:
CHAPTER 6 ?– SERVER CONTROL TEMPLATES 259
override protected void CreateChildControls()
{
Controls.Clear();
CreateControlHierarchy();
}
CreateControlHierarchy starts out by working with the HeaderTemplate template. The first
item that must always be checked is whether the template property has a value. This is detected
by examining the template property for a null value:
if (HeaderTemplate != null)
{
BasicTemplateContainer header = new BasicTemplateContainer();
HeaderTemplate.InstantiateIn(header);
Controls.Add(header);
Controls.Add(new LiteralControl("
"));
}
If the template property is null, a common feature of server controls is to render a generic
default HTML template so the output is consistent. The ASP.NET DataGrid control does this by
rendering a simple, plain HTML table when it is bound to a data source without templates. For
the HeaderTemplate template, we ignore the template and display nothing if it is null.
After the code checks for a null value of the template property, it next instantiates the
container that will serve as the host for the template content.
Pages:
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369