How to Bind Data to Repeater Control in Asp.net
Create Repeater Control Dynamically in VB.net
The 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 an HTML table and also can be used to prepare an 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 working 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.
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


Post A Comment:
0 comments: