Most Web server controls have a default look and layout, which you can manipulate by setting properties or by using styles. Some Web server controls also allow you to customize their look by using templates.
A template is a set of HTML elements and controls that make up the layout for a particular portion of a control. For example, in the
Note |
---|
Templates differ from styles. A template defines the content of a section of a control—for example, the contents of a row in the DataList control. Styles specify the appearance of elements such as color, font, and so on. Styles can apply to the control as a whole (for example, to set the font for the GridView control) as well as to template items. |
Templates consist of HTML and embedded server controls. When the control runs in the ASP.NET Web page, the control framework renders the contents of the template in place of the default HTML for the control.
Which Controls Support Templates?
Not all Web server controls support templates. For the most part, templates are supported by complex controls. This includes the GridView, DataList, and
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 GridView control, you can specify templates for columns (rather than rows).
The following table summarizes the Web server controls that support templates.
Control | Templates |
---|---|
|
|
DataList |
|
DetailsView |
|
FormView |
|
GridView |
|
|
|
Repeater |
|
|
|
|
|
Creating Templates
You can create templates directly in the .aspx file. Templates are created as XML declarations. 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 using data-bound controls.
Note |
---|
If you are using a visual designer, such as Visual Studio 2005, you can usually use a visual tool to create and edit templates. For more information, see |
Visual BasicВ | Copy Code |
---|---|
<asp:datalist id="DataList1" runat="server"> <HeaderTemplate> Employee List </HeaderTemplate> <ItemTemplate> <asp:label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'></asp:label> <asp:label id="Label2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.PhoneNumber")%>'></asp:label> <asp:Hyperlink id="Hyperlink1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>' NavigateURL='<%# DataBinder.Eval(Container, "DataItem.Link") %>'> </asp:Hyperlink> </ItemTemplate> </asp:datalist> |
C#В | Copy Code |
---|---|
<asp:datalist id="DataList1" runat="server"> <HeaderTemplate> Employee List </HeaderTemplate> <ItemTemplate> <asp:label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'></asp:label> <asp:label id="Label2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.PhoneNumber")%>'></asp:label> <asp:Hyperlink id="Hyperlink1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>' NavigateURL='<%# DataBinder.Eval(Container, "DataItem.Link") %>'> </asp:Hyperlink> </ItemTemplate> </asp:datalist> |