Web server controls such as the
![]() |
---|
For background information about templates, see ASP.NET Web Server Controls Templates |
To create a Web server control template using ASP.NET syntax
-
In the ASP.NET page, insert an element inside the control to identify what template you are creating, as shown in the following example:
Visual BasicВ Copy Code
<asp:DataList id="DataList1" runat="server"> <ItemTemplate> </ItemTemplate> </asp:DataList>
C#В Copy Code
<asp:DataList id="DataList1" runat="server"> <ItemTemplate> </ItemTemplate> </asp:DataList>
-
Inside the template element, add HTML text and other controls as the template's content. Include property and data-binding values for the embedded controls using normal syntax, as shown in the following example:
Visual BasicВ Copy Code
<asp:DataList id="DataList3" runat="server"> <ItemTemplate> Name: <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'/> </ItemTemplate> </asp:DataList>
C#В Copy Code
<asp:DataList id="DataList3" runat="server"> <ItemTemplate> Name: <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'/> </ItemTemplate> </asp:DataList>
-
Repeat Steps 1 and 2 for each template you want to create.
The following example shows a complete declaration for a DataList Web server control with simple templates declared for the Header, Item, and Separator templates.
Visual BasicВ Copy Code
<asp:datalist id="DataList2" runat="server" > <HeaderTemplate> Items matching your query: </HeaderTemplate> <ItemTemplate> Name: <asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'></asp:Label> </ItemTemplate> <SeparatorTemplate> <br><hr> </SeparatorTemplate> </asp:datalist>
C#В Copy Code
<asp:datalist id="DataList2" runat="server" > <HeaderTemplate> Items matching your query: </HeaderTemplate> <ItemTemplate> Name: <asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EmployeeName")%>'></asp:Label> </ItemTemplate> <SeparatorTemplate> <br><hr> </SeparatorTemplate> </asp:datalist>