Server Control Inline Template Syntax

Specifies what content will be included, and how it will be arranged and styled, within ASP.NET server controls that support templates.

<templatename>
      Server control, data-binding syntax, other valid markup
</templatename>

Markup Tags

  • templatename
    The name of the ASP.NET server control template.

Remarks

Templates can be used to structure and style the content that appears within ASP.NET server controls. Inline template syntax is a useful way to work with templates, because it enables you to design the arrangement and style of content in a control in simple declarative syntax, rather than doing it programmatically.

Many ASP.NET Web server controls support templates (for example, the Repeater, DataList, Gridview, Formview, MultiView, LoginView, and Menu controls all support templates, as do Web Parts zone controls including WebPartZone and CatalogZone). Note that only the templates that are specifically defined for a control can actually be declared for that control.

Template items must be declared as child elements of the server controls that support them. A server control can contain multiple template items, with each template used to specify the characteristics of a different content in the control. For example, the Repeater control supports templates to specify the layout and appearance of content within a header section, a body or item section, and a footer section, as well as special templates to handle the styles for alternating items and separators between items. For details, see the code example in this topic.

To specify how the content will appear within a template, you declare additional elements within the template tags. The additional markup can consist of HTML tags, ASP.NET controls, and inline server-side expressions or code blocks. For more information about how to use inline templates, see ASP.NET Web Server Controls Templates.

You can define your own templates when you develop custom server controls. For more information about developing controls that define and use inline style templates, see How to: Create Templated ASP.NET User Controls.

Example

The following code example shows how to declare the HeaderTemplate, AlternatingItemTemplate, ItemTemplate, and FooterTemplate templates in a Repeater Web server control. Each of these templates is associated with a property of the Repeater class.

<%@ Page Language="C#" %>
<html>
 <head>
    <script language="C#" runat="server">
       void Page_Load(Object Sender, EventArgs e) {
 
          if (!IsPostBack) {
             ArrayList values = new ArrayList();
 
             values.Add("Apple");
             values.Add("Orange");
             values.Add("Pear");
             values.Add("Banana");
             values.Add("Grape");
 
             Repeater1.DataSource = values;
             Repeater1.DataBind();
          }
       }
    </script>
 
 </head>
 <body>
 
    <h3><font face="Verdana">Repeater Example</font></h3>
 
    <form runat=server>
 
       <b>Repeater1:</b>
       <p>
         
       <asp:Repeater id=Repeater1 runat="server">
             
             
          <HeaderTemplate>
             <table border=1>
          </HeaderTemplate>
 
          <AlternatingItemTemplate>
             <tr>
                <td><b> <%# Container.DataItem %> </b> </td>
             </tr>
          </AlternatingItemTemplate>
 
          <ItemTemplate>
             <tr>
                <td> <%# Container.DataItem %> </td>
             </tr>
          </ItemTemplate>
 
          <FooterTemplate>
             </table>

          </FooterTemplate>
             
       </asp:Repeater>
       <p>
         
    </form>
 </body>
 </html>
<%@ Page Language="VB" %> 
<html>
<head>
<script language="VB" runat="server">

        Sub Page_Load(Sender As Object, e As EventArgs)
            
            If Not IsPostBack Then
                Dim values As New ArrayList()
                
                values.Add("Apple")
                values.Add("Orange")
                values.Add("Pear")
                values.Add("Banana")
                values.Add("Grape")
                
                Repeater1.DataSource = values
                Repeater1.DataBind()
            End If
        End Sub
</script>
 
 </head>
 <body>
 
    <h3><font face="Verdana">Repeater Example</font></h3>
 
    <form runat=server>
 
       <b>Repeater1:</b>
       <p>
         
       <asp:Repeater id=Repeater1 runat="server">
             
             
          <HeaderTemplate>
             <table border=1>
          </HeaderTemplate>
 
          <AlternatingItemTemplate>
             <tr>
                <td><b> <%# Container.DataItem %> </b> </td>
             </tr>
          </AlternatingItemTemplate>
 
          <ItemTemplate>
             <tr>
                <td> <%# Container.DataItem %> </td>
             </tr>
          </ItemTemplate>
 
          <FooterTemplate>
             </table>

          </FooterTemplate>
             
       </asp:Repeater>
       <p>
         
    </form>
 </body>
 </html>    

See Also

Tasks

How to: Create Templated ASP.NET User Controls

Reference

Custom Server Control Syntax

Concepts

ASP.NET Web Page Syntax Overview