ASP.Net Repeater


This article is written by Pon Saravanan  on 29-Nov-09 Last modified on :14-Jun-10





ASP.Net Repeater DataBinding Tutorial

Repeater can be used for listing data. It can be used to repeat the data horizontally or vertically. And can be used to form s a HTML table and also can be used to prepare a HTML List (LI). For displaying tabular data, we can use the ASP.Net Repeater to form the HTML Table. However when the ASP.Net Repeater is used as a container for controls, It is better to design the templates to be rendered with panels (<div>) and List Items (<LI> The DataList is also works similar to Repeater. But the DataList has some additional ability to perform than ASP.Net Repeater .

When the page renders, the control loops through the rows in the data source and renders the pre configured template (either in the markup or dynamically configured) for each row. Before and after processing the data items, the ASP.Net Repeater renders the header and the footer based on the configuration of the repeater. Because it has no default appearance, it is can be customized to list any layout and style. For myself if I have planned to work extensively on a page with HTML and strict CSS, I usually go for the ASP.Net Repeater control.

Templates in VB.Net Repeater

There are few templates that will be much helpful in repeater. Like other data bound controls Repeater also has some templates to work with. Templates in repeater are not much different from the rest of the Data Bound controls.
1. ItemTemplate
2. AlternatingItemTemplate
3. SeparatorTemplate
4. FooterTemplate
5. HeaderTemplate

There is a special template here introduced is SeparatorTemplate. This template can be used to add some elements between each row like a horizontal line.

ASP.Net Repeater DataBinding

Like other data bound controls, repeater also can be bound with a DataTable. So the procedure is to fetch the data to the DataTable using a SqlClient(if MS SQL server) and Use the SqlDataAdapter to fill it. Then the datatable can be assigned to the DataSource of the repeater. Finally instruct the repeater to bind using DataBind()

Source Code for ASP.Net Repeater

Markup (*.ASPX) for ASP.Net Repeater  

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater runat="server" ID="Repeater1">
        <HeaderTemplate>
            <asp:Label runat="server" ID="lblHeader" Text="Product"></asp:Label>
        </HeaderTemplate>
        <ItemTemplate>
           <li>
                <asp:Label runat="server" ID="lblItem"
                    Text='<%# DataBinder.Eval(Container.DataItem, "ProductName")%>'></asp:Label>
            </li>
        </ItemTemplate>
       
        <FooterTemplate>
           
        </FooterTemplate>
    </asp:Repeater>
    </form>
</body>
</html>

Code-Behind (*.VB)

Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
   
    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
        If (Not IsPostBack) Then
            Repeater1.DataSource = GetData(String.Empty)
            Repeater1.DataBind()
        End If
    End Sub
    Private ReadOnly Property ConnectionString() As String
        Get
            Return "Server=.\SQLEXPRESS;Database=NorthWind;Trusted_Connection=True"
        End Get
    End Property
    Private ReadOnly Property Connection() As SqlConnection
        Get
            Dim ConnectionToFetch As New SqlConnection(ConnectionString)
            ConnectionToFetch.Open()
            Return ConnectionToFetch
        End Get
    End Property
    Public Function GetData(ByVal Expression As String) As DataSet
        Dim SelectQry = "select * from Products"
        Dim SampleSource As New DataSet
        Dim SampleDataAdapter As New SqlDataAdapter(SelectQry, ConnectionString)
        SampleDataAdapter.Fill(SampleSource)
        Return SampleSource
    End Function
End Class










Comments
  • GUEST
    thank u man thnkq 5/5/2010 1:04:21 AM

  • GUEST
    thank u v much 4/24/2011 8:28:49 AM

  • GUEST
    THIS IS VERY FANTASTIC AND FINE INFORMATION. ASP.NET advanced is importent to evryone. 7/11/2011 4:23:02 AM

  • GUEST
    7/22/2011 3:31:13 AM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)