Binding Repeater to ObjectDataSource

by Jagadish Pulakhandam on 9/20/2011 11:22:41 AM
Rated 0 from 0 votes
Brief: Demonstrates on binding an ASP.NET Repeater control to ObjectDataSource control
Posted to: Data Binding using ObjectDataSource in ASP.NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Demonstrates the following:
  • Configuring ObjectDataSource to a business method
  • Modifying Repeater ItemTemplate to display data in the form of a table
  • Binding Repeater to ObjectDataSource
  • Binding without writing any code-behind.
Screen shot:


Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace NorthwindDataAccess.Models
07.{
08.    public class Customer
09.    {
10.        public string CustomerID { get; set; }
11.        public string CompanyName { get; set; }
12.        public string ContactName { get; set; }
13.        public string Address { get; set; }
14.        public string City { get; set; }
15.        public string Country { get; set; }
16.    }
17.}

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.using System.Data;
07.using NorthwindDataAccess.Models;
08.namespace NorthwindDataAccess.Factories
09.{
10.    public class CustomerFactory
11.    {
12.        public static List<Customer> GetList()
13.        {
14.            DataTable dtResult = DbHelper.GetResultSet("SELECT CustomerID, CompanyName, ContactName, Address, City, Country FROM Customers");
15.            return dtResult
16.                .AsEnumerable()
17.                .Select(dr => new Models.Customer()
18.                {
19.                    CustomerID = dr["CustomerID"].ToString(),
20.                    CompanyName = dr["CompanyName"].ToString(),
21.                    ContactName = dr["ContactName"].ToString(),
22.                    Address = dr["Address"].ToString(),
23.                    City = dr["City"].ToString(),
24.                    Country = dr["Country"].ToString(),
25.                })
26.                .ToList();
27.        }
28.    }
29.}

01.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NorthwindWebSite.WebForm1" %>
02. 
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05.<head runat="server">
06.    <title></title>
07.</head>
08.<body>
09.    <form id="form1" runat="server">
10.    <div>
11.        <table>
12.            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
13.                <ItemTemplate>
14.                    <tr>
15.                        <td>
16.                            Name:
17.                        </td>
18.                        <td>
19.                            <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ContactName") %>'></asp:Label>
20.                        </td>
21.                    </tr>
22.                    <tr>
23.                        <td>
24.                            Country:
25.                        </td>
26.                        <td>
27.                            <asp:Label ID="lblCountry" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Country") %>'></asp:Label>
28.                        </td>
29.                    </tr>
30.                </ItemTemplate>
31.                <SeparatorTemplate>
32.                    <tr>
33.                        <td colspan="2">
34.                            <hr />
35.                        </td>
36.                    </tr>
37.                </SeparatorTemplate>
38.            </asp:Repeater>
39.        </table>
40.        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetList"
41.            TypeName="NorthwindDataAccess.Factories.CustomerFactory"></asp:ObjectDataSource>
42.    </div>
43.    </form>
44.</body>
45.</html>


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following