|
Data binding refers to the process of dynamically assigning a value to a property of a control at runtime.The repeater control doesn't display anything unless it is bound to data source.The repeater control is used to display the records from the database.
The repeater control supports 5 templates:
• HeaderTemplate: Controls how the header of the repeater is formatted.
• ItemTemplate: Controls the formatting of each item displayed.
• AlternatingTemplate: controls how alternate items are formatted.
• SeparatorTemplate: display separator b/w each item displayed.
• FooterTemplate: Controls how footer of repeater is formatted.
Example of Binding Data to Repeater Contrrol
Note:- The example will bind the data to repeater control and the display of the repeater control is controlled by the different templates that are there with this control. The example will display the alternate rows in different colours.
RepeaterControl.aspx
<% @ Page Language ="VB" AutoEventWireup ="false" CodeFile ="Example14.aspx.vb" Inherits ="Example14" %>
<! 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 runat ="server">
</ head >
< body >
< form id ="form1" runat ="server">
< asp : Repeater ID ="Repeater1" runat ="server">
< HeaderTemplate >
< table border =1>< tr >
< th > First Name </ th >
< th > Last Name </ th >
< th > Phone </ th ></ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >< td > <% # Container.DataItem( "au_fname" ) %> </ td >
< td > <% # Container.DataItem( "au_lname" ) %> </ td >
< td > <% # Container.DataItem( "phone" ) %> </ td >
</ tr ></ ItemTemplate >
< AlternatingItemTemplate >
< tr bgcolor =blue>< td >
<% # Container.DataItem( "au_fname" ) %> </ td >
< td > <% # Container.DataItem( "au_lname" ) %> </ td >
< td > <% # Container.DataItem( "phone" ) %> </ td >
</ tr >
</ AlternatingItemTemplate >
< FooterTemplate >
</ table >
</ FooterTemplate >
</ asp : Repeater >
</ form >
</ body >
</ html >
RepeaterControl.aspx.vb
Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Example14 Inherits System.Web.UI.Page
Protected Sub Page_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim dtr As SqlDataReader
con = New SqlConnection( "Data Source=localhost;Initial catalog=pubs;UID=sa;pwd=sa" )
cmd = New SqlCommand( "Select * from Authors" , con)
con.Open()
dtr = cmd.ExecuteReader
Repeater1.DataSource = dtr
Repeater1.DataBind()
dtr.Close()
con.Close()
End Sub
End Class
|