The ASP.NET Beta1 framework provides three data-bound web controls for presentation of data.
- Repeater
- DataList
- DataGrid
This article covers implementation of each web control as they relate to databinding. The Repeater control will be explained first. All code is written in VB.
RepeaterThe Repeater class utilizes templates. You define the style for the header, footer, and detail blocks. Additionally, you may specify a template for the separation of detail lines and a template for the style of every other detail line (i.e. greenbar paper effect).
Repeater example using HTML bullets:
<asp:Repeater id=myRepeater runat=server> <template name="HeaderTemplate"> <ul> </template>
<template name="ItemTemplate"> <li><%# Container.DataItem("CustomerName")%></li> </template>
<template name="FooterTemplate"> </ul> </template> </asp:Repeater>
|
Take note of this magical (and rather odd looking) statement below:
<%# Container.DataItem("CustomerName")%> This single statement performs the task of internally looping the bound datastore and displaying the output. For a Repeater control to work, it must bind to a datasource (for example:
ADODataSetCommand class) via the DataSource property of the Repeater control. For my datasource, I have chosen to use the
DataView class. And for simplicity, the data will be generated with inline code.
<%@ Import namespace="System.Data" %> <html> <script language="VB" runat="server">
Sub Page_Load(s As System.Object, e As System.EventArgs)
Dim myDataTable As DataTable Dim myDataView As DataView Dim myDataRow As DataRow
myDataTable = New DataTable 'member of System.Data namespace myDataTable.Columns.Add(New DataColumn("CustomerName", GetType(String)))
myDataRow = myDataTable.NewRow() myDataRow(0) = "Francis Gillom" myDataTable.Rows.Add(myDataRow)
myDataRow = myDataTable.NewRow() myDataRow(0) = "Jessica Klein" myDataTable.Rows.Add(myDataRow)
myDataRow = myDataTable.NewRow() myDataRow(0) = "Duane Dodge" myDataTable.Rows.Add(myDataRow)
myDataView = new DataView(myDataTable)
myRepeater.DataSource = myDataView myRepeater.DataBind End Sub
</script> </head> <body>
<asp:Repeater id=myRepeater runat=server> <template name="HeaderTemplate"> <ul> </template>
<template name="ItemTemplate"> <li><%# Container.DataItem("CustomerName")%></li> </template>
<template name="FooterTemplate"> </ul> </template> </asp:Repeater>
</body> </html>
|