Article
Beginning ASP.NET Using VB.NET - Chapter 14: ASP.NET Server Controls
DataGrid
As well as allowing you to create a grid, the DataGrid control also lets you format its columns and rows to control the layout of your grid, using templates (see the DataList control for a list of some of the templates). For example, you could alternate the colors for the rows of data being displayed. As well as templates, this control supports several interesting properties, which include:
- AllowSorting: enables you dynamically sort and re-display the data based on a selected column. For example, if you had a table containing your employees surnames and salaries, enabling sorting would allow you to sort your table according to either column.
- AllowPaging: the ability to view subsets of the data called by the
DataGridcontrol on different pages. The number of items displayed on the page is determined by thePageSizeproperty. - AlternatingItemStyle: the style (such as background colour) of every other item listed.
- FooterStyle: the style of the footer at the end of the list (if any).
- HeaderStyle: the style of the header at the beginning of the list (if any) .
- ItemStyle: the style of individual items.
To use the DataGrid control, you have to specify it within tags, set the relevant properties, define the columns in your table, and then apply the relevant template for those columns. Within the template tags, you include the information the template must be applied to:
<asp:DataGrid id="EventData"
AllowSorting="true"
<Columns>
<asp:TemplateColumn HeaderText="Column1">
<ItemTemplate>
<%# Container.DataItem("ShortDesc") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Column2">
<ItemTemplate>
<%# Container.DataItem("DetailDesc") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
We'll see the DataGrid control in action in the last Try It Out of this chapter.
DataList
The DataList control is useful for displaying rows of database information (which can become columns in DataGrid tables) in a format that you can control very precisely using templates and styles. Manipulating various template controls changes the way your data is presented. The DataList control enables you to select and edit the data that is presented. The following is a listing of some supported templates:
ItemTemplate:required template that provides the content and layout for items referenced byDataList.AlternatingItemTemplate:if defined, this template provides the content and layout for alternating items in theDataList. If not defined,ItemTemplateis used.EditItemTemplate: if defined, this template provides editing controls, such as text boxes, for items set to 'edit' in theDataList. If not defined,ItemTemplateis used.FooterTemplate: if defined, theFooterTemplateprovides the content and layout for the footer section of theDataList. If not defined, a footer section will not be displayed.HeaderTemplate: if defined, this provides the content and layout for the header section of theDataList. If not defined, a header section will not be displayed.SelectedItemTemplate: if defined, this template provides the content and layout for the currently selected item in theDataList. If not defined,ItemTemplateis used.SeparatorTemplate: if defined, this provides the content and layout for the separator between items in theDataList. If not defined, a separator will not be displayed.
To use the DataList control and its templates, you have to specify it in tags, and then specify your templates within tags as well. Unlike the DataGrid, items to be affected by the template occur within the template tags. Each template has its own set of properties, which you can find in the .NET documentation (and remember to be sure to have defined your DataSource):
<asp:DataList id="DataList1" runat="server">
<FooterTemplate>
'Items to be affected by this template
</FooterTemplate>
<SeparatorTemplate>
'Items to be affected by this template
</SeparatorTemplate>
</asp:DataList>
If you use the DataList control to set up a column in a grid, you can set additional templates within that column by using the DataGrid control. Refer to the ASP.NET documentation for more information.
Repeater
The Repeater control is very similar to the DataList control with one very important distinction: the data displayed is always read only -- you cannot edit the data being presented. It is particularly useful for displaying repeating rows of data. Like the DataGrid and DataList controls, it utilizes templates to render its various sections. The templates it uses are generally the same as the ones used with the DataList control, and the syntax is also the same. We'll see an example of the Repeater control in action in the next Try It Out.
We've now looked at almost everything we're going to in this chapter. All that is left to do is to bring everything together in two exercises, the first of which creates a dynamic calendar, and the second one uses the EditItemTemplate to create an editable able of information.