Template programming for web server-side controls
When developing Windows and Web GUI programs, we use the data binding control. Fortunately, Windows Forms and Web Forms offer rich data binding controls, we can use them with very rich data-driven programs.
Data binding controls are not only used to display data, and it can also display different layouts by layout features of the data binding control. In the server-side data binding control, Templates is used to customize the formats and layouts of the data displayed by our own needs.
Introduction
Templates plays a major role in the format and layout of the ASP.NET data binding control. Before we use these Templates, let's first give these basic templates and how to make a quick preview.
Templates provides a complicated data formatted by tagging by using HEADER, FOOTER, and ITEMS. Template is a series of HTML tag sets that define the style of a specific part of the control. In the HTML tag, a template can also contain other controls and ASP.NET INLINE CODE.
OK, for us better understanding, let us see a simple example of using templates. The code has been listed in LIST1. This example uses the REPEATER control, which uses AlternatingTemplate and ItemTemPate two templates. We will further discuss the use of different templates in the next part. In AlternatingTemplate, we set up font color = red, face = verdana and size = 3, in itemtempate, we set color = green, face = tahoma, and size = 2. And use HTML table tags. From the above settings, you can see how the rows and alternate rows of this REPEATER control will display.
Listing 1. Applying a Simple Template on a Repeater Control
alternating data tr> table> font> alternatingitemtemplate> Some item tr> table> font> itemtemplate > footertemplate> asp: repeater> p>
Template type (Template Types)
Template describes the layout and style of each part of the control. Just like other control tags, each pair of Templates also have labels that start and close. In the example, HeadeRTemplate describes the style and layout of the header of the control. headertemplate> represents the header of the control. The following table lists a variety of templates. Table 1. ASP.NET TEMPLATES
Template
Description
Headertemplate
Describe the layout and style of the control head.
Footertemplate
Describes the layout and style of the tetail of the control.
ItemTemplate
Describe the layout and style of each of the controls.
AlternatingItemTemplate
Describe the layout and style of the alternating item of the control.
SeparatorTemplate
The separation style and layout between each item are described.
SelectedItemTemplate
Describe the style and layout of the selected item.
EditItemTemplate
Some controls support editing. He describes the style and layout of the item being edited.
Pager Template
The page layout and style of the DataGrid control.
Which Controls Support Templates?
Templates is not supported by all data binding controls all of ASP.NET, which is just supported by those complex controls. It is particularly important to point out that different controls support Templates and features of different collections.
E.g:
The Repeater control supports HeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, SeperatorTemplate.
The DataList control supports HeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, SeparatorTemplate, SELECTEMTEMPLATE, and EditItemTemplate.
The DataGrid control supports HeaderTemplate, FooterTemplate, ItemTemplate, EditItemPlate, and Pager.
Creating Templates
You can reply to the Templates code directly in the code of the ASPX page.
In Listing 1, we can see the code that can easily add Templates tags and formatted code. For example, if you want to add Templates to Datalist, you must first know which templates can be used. DataList supports Header, Footer, Item, Alternating Item, Selected Item, Edit Item Templates.
Let's take a look at the examples using Header, Item, Alternating Item and Footer Templates. The code is listed in LIST2. DataList's REPEATECOLUMNS attribute defines the number of rows that can be displayed.
Listing 2. Adding Templates to a Datalist Control
HeaderTemplate> AlternatingItemTemplate> ItemTemplate> < / Footertemplate> asp: DataList> The result of the code running on the above code is nothing, the formatting code of each part of the DataList control requires us to add it. Listing3 is an extension of an example above, where we have added specific formatted HTML tags and code.
Listing 3. Adding Templates Format of A Datalist Control
DataList control header b>
category ID: b> <% # databinder.eval ("categoryID")%>
category name: b> <% # DataBinder.eval (Container.DataItem, "categoryName")%>
Description: b> <% # databinder.eval (container.DataItem, "description")%>
Picture: b> <% # databinder.eval (Container.DataItem, "Picture")%> div> font> alternatingitemtemplate>
category ID: b> <% # DataBinder.eval (Container.DataItem, "categoryID")%>
category name: b> <% # databinder. Eval (Container.DataItem, "CategoryName")%>
Description: b> <% # databinder.eval (Container.DataItem, "Description")%>
Picture: < / b> <% # databinder.eval (Container.DataItem, "Picture")%> div> DataList Control Footer font> footertemplate> Templates in action
OK, let's use DataList's data to populate these templates. The code is listed in Listing4.
We use a method FillData, which is used to bind the data of the Northwind database to the DataList control.
Listing 4.
<% @ Import namespace = "system.data"%> <% @ import namespace = "system.data.sqlclient"%>
DataList Server Control Sample font>