Data binding in ASP.NET Repeater control from code-behind using DataView

By | August 17, 2013

Introduction

In this article I will explain how to bind data to a Repeater control in ASP.NET from code-behind using DataView to render a DIV layout

Description

In this article we will create a DIV layout using Repeater control to display product list with their images. We will use DataView to sort and bind data to the Repeater control from the code-behind.

First we will create a table with some sample data. Then we will use SqlDataAdapter and DataTable to put data in a DataView object. After that we will use “ItemTemplate” template of the Repeater control to create a DIV layout. Finally we will sort data using the DataView object and bind data to the Repeater control programmatically from the code-behind.

Step 1: Create a table “NewProducts” in SQL Server with following fields and data

New Products

Step 2: Create a new ASP.NET application

Step 3: Add a ConnectionString “ConString” in the Web.config as per your database details

<connectionStrings>
     <add name="ConString" 
        connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;User Id=sa;Password=********;"/>
</connectionStrings>

Step 4: Add a folder “Images” in the project and add images in the folder with the names as in the “NewProducts” table

Step 5: Add a Repeater control on the form and add following in the HTML source

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div style="float: left; width: 202px; margin-top: 15px; padding-left: 15px;">
            <div style="height: 200px; border: 1px solid #CCCCCC;">
                <img width="200px" height="200px" src=" <%#Eval("ProductPhoto") %>" alt="<%#Eval("ProductName") %>">
            </div>
            <div style="clear: both;">
            </div>
            <div class="title">
                <%#Eval("ProductName") %>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

Step 6: Add following in the code-behind of the form

C#

protected void Page_Load(object sender, EventArgs e)
{
    BindRepeater();
}

private void BindRepeater()
{
    using(SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
    {
        SqlDataAdapter sda = new SqlDataAdapter("SELECT ProductName, ProductPhoto FROM NewProducts", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        DataView dv = new DataView(dt);
        dv.Sort = "ProductName";    // Sort by product name
        Repeater1.DataSource = dv;
        Repeater1.DataBind();                
    }
}

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    BindRepeater()
End Sub

Private Sub BindRepeater()
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConString").ConnectionString)
        Dim sda As New SqlDataAdapter("SELECT ProductName, ProductPhoto FROM NewProducts", con)
        Dim dt As New DataTable()
        sda.Fill(dt)

        Dim dv As New DataView(dt)
        dv.Sort = "ProductName" 'Sort by product name
        Repeater1.DataSource = dv
        Repeater1.DataBind()
    End Using
End Sub

Here, DataView is used to sort data of the DataTable and then set as the DataSource of the Repeater control.

Complete Source Code:

HTML Source

<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
    <style type="text/css">
        .title
        {
            background-color: #FFFFFF;
            border: 1px solid #CCCCCC;
            bottom: 1px;
            left: 1px;
            color: #1A1A1A;
            font-size: 13px;
            font-weight: bold;
            padding: 8px 0 8px 0px;
            text-transform: uppercase;
            width: 200px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 900px;">
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <div style="float: left; width: 202px; margin-top: 15px; padding-left: 15px;">
                    <div style="height: 200px; border: 1px solid #CCCCCC;">
                        <img width="200px" height="200px" src=" <%#Eval("ProductPhoto") %>" alt="<%#Eval("ProductName") %>">
                    </div>
                    <div style="clear: both;">
                    </div>
                    <div class="title">
                        <%#Eval("ProductName") %>
                    </div>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Code Behind C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace RepeaterExample
{
    public partial class RepeaterExample2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindRepeater();
        }

        private void BindRepeater()
        {
            using(SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
            {
                SqlDataAdapter sda = new SqlDataAdapter("SELECT ProductName, ProductPhoto FROM NewProducts", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);

                DataView dv = new DataView(dt);
                dv.Sort = "ProductName";    // Sort by product name
                Repeater1.DataSource = dv;
                Repeater1.DataBind();                
            }
        }
    }
}

Code Behind VB.NET

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class RepeaterExample2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindRepeater()
    End Sub

    Private Sub BindRepeater()
        Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConString").ConnectionString)
            Dim sda As New SqlDataAdapter("SELECT ProductName, ProductPhoto FROM NewProducts", con)
            Dim dt As New DataTable()
            sda.Fill(dt)

            Dim dv As New DataView(dt)
            dv.Sort = "ProductName" 'Sort by product name
            Repeater1.DataSource = dv
            Repeater1.DataBind()
        End Using
    End Sub
End Class

Output:

Repeater Control Div Layout

0 thoughts on “Data binding in ASP.NET Repeater control from code-behind using DataView

  1. QuentinBak

    Your comment is awaiting moderation.

    Many thanks! I appreciate it.
    http://canadianpharmacymim.com/
    canadian pharmacy meds northwestpharmacy online pharmacies canada canadian pharmacies without an rx
    online canadian discount pharmacy
    <a href="http://www.ikeaclub.biz.ua/lib/ajax/contact_us_form.php?form_ajax=contacts&name=Quentinnax&email=zittel020eejf%40songwriter.net&phone=81296888622&message=+Lovely+data%2C+Thanks.+http%3A%2F%2Fcanadianpharmacyntx.com%2F+canadian+pharmacy+without+prescription+online+pharmacies+of+canada+canadapharmacy+com+canadian+pharmaceuticals+online+%0D%0Apharmacy+drug+store+%0D%0Acandrugstore+com+%0D%0Acanadapharmacyonline+com+%0D%0Awalmart+pharmacy+price+check+%0D%0Acanada+online+pharmacies+%0D%0Amedical+information+online&security_code=27887497&send=”>online pharmacy usa
    online canadian pharmacy
    pharmacy online mexico
    canada pharmacy
    cialis canadian pharmacy

    Reply
  2. QuentinBak

    Your comment is awaiting moderation.

    Thanks, Good information!
    https://nicktambone.com/
    24 hour pharmacy canada drug board of pharmacy canadian pharmacies online
    canadian pharmacy
    the canadian pharmacy
    mail order pharmacy
    discount prescription drug
    <a href="http://mirabiliatour.blogspot.com/ask?q=+Whoa+a+good+deal+of+superb+information.+http%3A%2F%2Fcanadianonlinepharmacynnm.com%2F+rx+price+comparison+northwestpharmacy+canada+pharmacy+online+no+script+%5Burl%3Dhttp%3A%2F%2Fcanadianonlinepharmacynnm.com%2F%5Dcanadianpharmacy%5B%2Furl%5D+%0D%0A24+hour+pharmacy+%0D%0Apharmacy+cost+comparison+%0D%0Aprescription+drugs+canada+%0D%0Aonline+canadian+pharmacy+%0D%0Acanadian+pharmacy+reviews+%0D%0Acanadian+online+pharmacies&submit=ask”>pharmacy near me
    pain meds online without doctor prescription

    Reply
  3. QuentinBak

    Your comment is awaiting moderation.

    Really quite a lot of fantastic advice.
    http://canadianpharmaciescubarx.com/
    online pharmacies north west pharmacy canada canadian pharmacy reviews canadian pharmacy online
    canadian online pharmacy
    drugs without prescription
    legitimate online pharmacies
    canadian pharcharmy online
    <a href="http://www.oyunuzayi.com/blog/?s=&submit=&comment=+With+thanks%21+Lots+of+tips.++http%3A%2F%2Fwww.canadianpharmacymsn.com%2F+pain+meds+online+without+doctor+prescription+canadian+pharmacies-24h+online+pharmacies+no+prescription+canada+pharmaceuticals+online+%0D%0Aprescription+drugs+online+without+doctor+%0D%0Adiscount+pharmacy+%0D%0Abest+price+prescription+drugs+%0D%0Acanadian+pharmacies+top+best+%0D%0Acanadian+rx+%0D%0Acanadian+pharmacy+reviews&author=QuentinMaign&email=zittel020eejf%40songwriter.net&url=http%3A%2F%2Fhttps%3A%2F%2Fnicktambone.com%2F&passthis=2U8%5Esok9lxB&comment_post_ID=1421&comment_parent=0″>canada pharmacies online prescriptions
    online pharmacy canada

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *