Quantcast
Viewing all articles
Browse latest Browse all 2

Data binding in ASP.NET Repeater control using SQLDataSource control

Introduction

In this article I will explain how to bind data to a repeater control in ASP.NET

Description

Repeater control displays repeated set of data from the data source. It can display data in any form as per our need, in a table layout, a div layout, a comma-separated list, an xml format, ordered list, unordered list etc. Repeater control is completely template based. It does not have any default layout. So we need to define a template to display data in the Repeater control.

Repeater control supports five templates, HeaderTemplate, ItemTemplate, AlternatingItemTemplate, SeparatorTmplate and FooterTemplate

Template Description
HeaderTemplate HeaderTemplate renders header of the Repeater control. It is called once for every Repeater control. For example, in a table layout it can be used to render opening <table> tag.
ItemTemplate ItemTemplate renders data row of the Repeater control. It is called for every record in the data source. For example, it can be used to render data records in <tr><td></td></tr> tags for every rows.
AlternatingItemTemplate AlternatingItemTemplate is called for every alternate record in the data source. For example, it can be used to change background of every alternate rows.
SeparatorTemplate SeparatorTemplate is used to provide a separator between records. For example, a horizontal ruler can be used to separate rows of the Repeater control.
FooterTemplate FooterTemplate is used to render footer of the Repeater control. It is also called once for every Repeater control like HeaderTemplate. For example, it can be used to render closing </table> tag.

Step 1:

Create a New ASP.NET project

Image may be NSFW.
Clik here to view.
ASP.NET New Project

Step 2:

Drag a SqlDataSource control on the form and write following

<%--Add a connection string "ConString" in the web.config as per your database--%>
<asp:SqlDataSource
    ID="SqlDataSource1"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:ConString %>"
    SelectCommand="SELECT TOP 10 EmployeeID, LastName, FirstName, DepartmentID, Salary, HireDate FROM Employees">
</asp:SqlDataSource>

SqlDataSource is used to get data from the Employees table which we will bind to the Repeater control. You can use any other table.

Step 3:

Drag a Repeater control on the form and write following in the HTML source to render Repeater control as table

<asp:Repeater
    ID="Repeater1"
    runat="server"
    DataSourceID="SqlDataSource1">
    <HeaderTemplate>
        <table>
            <tr style="background-color:Purple; color:White;">
                <td>Employee ID</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Department ID</td>
                <td>Salary</td>
                <td>Hire Date</td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%#Eval("EmployeeID") %></td>
            <td><%#Eval("FirstName") %></td>
            <td><%#Eval("LastName") %></td>
            <td><%#Eval("DepartmentID") %></td>
            <td><%#Eval("Salary") %></td>
            <td><%#Eval("HireDate") %></td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr style="background-color:Silver;color:White;">
            <td><%#Eval("EmployeeID") %></td>
            <td><%#Eval("FirstName") %></td>
            <td><%#Eval("LastName") %></td>
            <td><%#Eval("DepartmentID") %></td>
            <td><%#Eval("Salary") %></td>
            <td><%#Eval("HireDate") %></td>
        </tr>
    </AlternatingItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

Here, we are using a SqlDataSource control as the data source of the Repeater control so we have set DataSourceID of the Repeater control to SqlDataSource1. In the <HeaderTemplate>, opening tag of table and the table header is rendered. In the <ItemTemplate>, one table row is defined which uses Eval() method to get data from the data source. This block is repeated for every row in the data source and prints all the rows. <AlternatingItemTemplate> is used to change the background color of alternate rows.  And <Footer> template is used to render the closing tag of the table.

Complete HTML Source:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="RepeaterExample.WebForm1" %>
<!DOCTYPE>
<html>
<head runat="server">
    <title></title>
    <style type="text/css">
        table
        {
            width:600px;
            border-spacing:0;
            border-collapse:collapse;
            border:1px solid navy;
        }
        td
        {
            width:100px;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater
        ID="Repeater1"
        runat="server"
        DataSourceID="SqlDataSource1">
        <HeaderTemplate>
            <table>
                <tr style="background-color:Purple; color:White;">
                    <td>Employee ID</td>
                    <td>First Name</td>
                    <td>Last Name</td>
                    <td>Department ID</td>
                    <td>Salary</td>
                    <td>Hire Date</td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%#Eval("EmployeeID") %></td>
                <td><%#Eval("FirstName") %></td>
                <td><%#Eval("LastName") %></td>
                <td><%#Eval("DepartmentID") %></td>
                <td><%#Eval("Salary") %></td>
                <td><%#Eval("HireDate") %></td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr style="background-color:Silver;color:White;">
                <td><%#Eval("EmployeeID") %></td>
                <td><%#Eval("FirstName") %></td>
                <td><%#Eval("LastName") %></td>
                <td><%#Eval("DepartmentID") %></td>
                <td><%#Eval("Salary") %></td>
                <td><%#Eval("HireDate") %></td>
            </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

    <%--Add a connection string "ConString" in the web.config as per your database--%>
    <asp:SqlDataSource
        ID="SqlDataSource1"
        runat="server"
        ConnectionString="<%$ ConnectionStrings:ConString %>"
        SelectCommand="SELECT TOP 10 EmployeeID, LastName, FirstName, DepartmentID, Salary, HireDate FROM Employees">
    </asp:SqlDataSource>
    </form>
</body>
</html>

Output:

Image may be NSFW.
Clik here to view.
Repeater control table layout


Filed under: .Net, ASP.NET Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 2

Trending Articles