Databound Controls in ASP.NET

Introduction and Demonstration

Databound controls render repetitive data and use templates for the customized rendering of data. For example, you can define separate templates for the header, body, and footer of a table of data.

ControlPurpose
DataGridDisplays tabular data and can function as an editable grid that supports selecting, editing, sorting, and paging of data.
DataListDisplays a list of items that the user can select and edit.
RepeaterDisplays a repeating list of data items. Their control and layout can be specified using templates.

Remember that DataGrid and DataList controls can be used with or without templates, but Repeater controls must have at least one ItemTemplate defined. You can also modify the appearance of DataGrids and DataLists by using a series of style properties, each of which ends with Style. These properties include HeaderStyle, ItemStyle, and FooterStyle.

The simplified syntax of the databound controls is as follows:

<asp:datagrid id="MyDataGrid" allowpaging="true" allowsorting="true" alternatingitemstyle-backcolor="LightSkyBlue"
    backcolor="Blue" forecolor="White" cellpadding="2" cellspacing="0" headerstyle-backcolor="DarkBlue"
    headerstyle-forecolor="Yellow" pagerstyle-mode="NumericPages" pagesize="5" runat="server" />
<asp:datalist id="MyDataList" alternatingitemstyle-backcolor="LightSkyBlue" backcolor="Blue"
    bordercolor="Black" cellpadding="2" cellspacing="0" forecolor="White" headerstyle-backcolor="DarkBlue"
    headerstyle-forecolor="Yellow" repeatcolumns="1" repeatdirection="vertical" repeatlayout="table"
    runat="server">
<template name="headertemplate">
Composers
</template>
<
template name="itemtemplate">
<%# databinder.eval(container.dataitem, "name") %>
</template>
</
asp:datalist>
<
asp:repeater id="MyRepeater" runat="server"><template name="headertemplate">
<table cellpadding="5" cellspacing="0">
<tr>
<
td>Name<hr/></td>
<td>City<hr/></td>
</tr>
</
template>
<
template name="itemtemplate">
<tr>
<
td><%# DataBinder.Eval(Container.DataItem, "name") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "city") %></td>
</tr>
</
template>
<
template name="footertemplate">
</table>
</
template>
</
asp:repeater>

The controls can then be referenced programmatically with code fragments like:

MyDataGrid.DataSource = CreateData( )
MyDataGrid.DataBind( )
MyDataList.DataSource = CreateData( )
MyDataList.DataBind( )
MyRepeater.DataSource = CreateData( )
MyRepeater.DataBind( )

Index.aspx Page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
<%@ Import Namespace="System.Data" %>
<html>
<
head>
    <title>Databound Control Example</title>
    <script runat="server">
Sub Page_Load( )
MyDataGrid.DataSource = CreateData( )
MyDataGrid.DataBind( )
MyDataList.DataSource = CreateData( )
MyDataList.DataBind( )
MyRepeater.DataSource = CreateData( )
MyRepeater.DataBind( )
End Sub
Function CreateData( ) As DataTable
Dim DT As New DataTable( )
Dim Row1, Row2, Row3, Row4 As DataRow
DT.Columns.Add(New DataColumn("name", _
System.Type.GetType("System.String")))
DT.Columns.Add(New DataColumn("city", _
System.Type.GetType("System.String")))
Row1 = DT.NewRow( )
Row1("name") = "W.A. Mozart"
Row1("city") = "Salzburg"
DT.Rows.Add(Row1)
Row2 = DT.NewRow( )
Row2("name") = "Nikolai Rimsky-Korsakov"
Row2("city") = "Tikhvin"
DT.Rows.Add(Row2)
Row3 = DT.NewRow( )
Row3("name") = "George Frideric Handel"
Row3("city") = "Halle"
DT.Rows.Add(Row3)
Row4 = DT.NewRow( )
Row4("name") = "J.S. Bach"
Row4("city") = "Eisenach"
DT.Rows.Add(Row4)
Return DT
End Function
    </script
>
</head>
<
body>
    <h1>
        Databound Control Example</h1
>
    <form id="Form1" runat="server">
    <asp:Table ID="MyTable" border="1" CellPadding="5" CellSpacing="0" runat="server">
        <asp:TableRow ID="Tablerow1" runat="server">
            <asp:TableCell ID="Tablecell1" runat="server">
DataGrid Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell2" runat="server">
                <asp:DataGrid ID="MyDataGrid" AllowPaging="true" AllowSorting="true" AlternatingItemStyle-BackColor="LightSkyBlue"
                    BackColor="Blue" ForeColor="White" CellPadding="2" CellSpacing="0" HeaderStyle-BackColor="DarkBlue"
                    HeaderStyle-ForeColor="Yellow" PagerStyle-Mode="NumericPages" PageSize="5" runat="server" />
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow2" runat="server">
            <asp:TableCell ID="Tablecell3" runat="server">
DataList Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell4" runat="server">
                <asp:DataList ID="MyDataList" AlternatingItemStyle-BackColor="LightSkyBlue" BackColor="Blue"
                    BorderColor="Black" CellPadding="2" CellSpacing="0" ForeColor="White" HeaderStyle-BackColor="DarkBlue"
                    HeaderStyle-ForeColor="Yellow" RepeatColumns="1" RepeatDirection="vertical" RepeatLayout="table"
                    runat="server">
                    <HeaderTemplate>
                        Composers
                    </HeaderTemplate
>
                    <ItemTemplate>
                        <%# databinder.eval(container.dataitem, "name") %>
                    </ItemTemplate>
                </asp:DataList>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow3" runat="server">
            <asp:TableCell ID="Tablecell5" runat="server">
Repeater Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell6" runat="server">
                <asp:Repeater ID="MyRepeater" runat="server">
                    <HeaderTemplate>
                        <table cellpadding="5" cellspacing="0">
                            <tr>
                                <td>
                                    Name<hr />
                                </td>
                                <td>
                                    City<hr />
                                </td>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, _"name") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, _"city") %>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    </form>
</body>
</
html>

HAVE A HAPPY CODING!


Similar Articles