Continually Improving.... let us know how support@devdiamond.net Sign in | Sign up
home articles news blog forum  

 
 


 
Skip Navigation LinksHome > Article > ASP.Net :: Databinding With List-bound Web Controls
ASP.Net :: Databinding With List-bound Web Controls
Abstract :
Databinding With List-bound Web Controls within ASP.Net framework

Views : 6270
Published : Friday, February 22, 2002
By
HyperLink

Avarage Rating :
Page Page 1 of 2

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.

Repeater

The 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>


About Author

        Yasir Send Feedback
        Yasir is a .NET expert, with over 5 years experience in Microsoft Technologies, 8 years overall programming experience, he is the owner, founder & primary contributor of Minwar.com, and he also works as IT Director in the hospitality industry.