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.
|
Control
|
Purpose
|
|
DataGrid
|
Displays tabular data and can function as an editable
grid that supports selecting, editing, sorting, and paging of data.
|
|
DataList
|
Displays a list of items that the user can select and
edit.
|
|
Repeater
|
Displays 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" %>
<%@
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 runat="server">
<asp:table id="MyTable"
border="1"
cellpadding="5"
cellspacing="0"
runat="server">
<asp:tablerow
runat="server">
<asp:tablecell runat="server">
DataGrid Control:
</asp:tablecell>
<asp:tablecell
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
runat="server">
<asp:tablecell
runat="server">
DataList Control:
</asp:tablecell>
<asp:tablecell
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
runat="server">
<asp:tablecell
runat="server">
Repeater Control:
</asp:tablecell>
<asp:tablecell
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>
Comments
Post a Comment