All Web Forms controls have a default look and layout, which you can manipulate by setting properties or by using styles. Some Web controls also allow you to customize their look by using templates.
A template is a set of HTML elements an controls that make up the layout for a particular portion of a control. For example, in the DataList Web control you can use a combination of HTML elements and controls to create the layout for each row of the list.
has a default look for rows in the grid. However, you can customize the grid's look by defining different templates for individual rows, alternating rows, selected rows, and so on.
Note Templates differ from styles. Styles specify the appearance of elements such as color, font, and so on within a control's layout. You can use styles without using templates, if all you want to do is change the appearance of the default layout of a control. You can also use styles with a template to control the appearance of elements that you define within your templates.
Templates consist of HTML and even embedded Web Forms controls. When the control runs in the Web Forms page, the control framework renders the contents of the template in place of the default HTML for the control.
Not all Web Forms controls support templates. For the most part, templates are supported by complex controls. In Visual Studio, this includes the DataGrid, DataList, and Repeater Web controls. (In fact, the Repeater control not only supports templates, but requires that you create a template to define its output.) Controls that you acquire from other sources might also support templates.
Each control supports a slightly different set of templates that specify layouts for different portions of the control, such as the header, footer, item, and selected item. You can specify a template for any or all of these, depending on which ones you want to customize. In the DataGrid control, you can specify templates for columns (rather than rows).
The following table summarizes the Web Forms controls that support templates
Control | Templates |
---|---|
Repeater |
|
DataList |
|
DataGrid |
|
You can create templates directly in the .aspx file. Templates are created as XML declarations. A special element— the <template>
element— contains the information to be rendered for the template. The template type (for example, ItemTemplate) is provided as the NAME attribute of the template element.
The following example shows how you can display a list of employee names, phone numbers, and email addresses using templates in the DataList control. The layout of the employee information is specified in the ItemTemplate template using data-bound controls.
<asp:DataList ID="DataList1" runat="Server"> <template name="HeaderTemplate"> Employee List </template> <template name="ItemTemplate"> <asp:Label runat=server Text="<%# Container.DataItem.EmployeeName %>" /> <asp:Label runat=server Text="<%# Container.DataItem.PhoneNumber %>" /> <asp:HyperLink runat=server Text="<%# Container.DataItem.Email %>" NavigateURL="<%# Container.DataItem.Link %>" />) </template> </asp:DataGrid>
Web Forms Server Controls | Web Forms Controls and CSS Styles | Web Forms Data Binding