Binding DataList to ObjectDataSource

by Jagadish Pulakhandam on 9/20/2011 5:48:11 AM
Rated 0 from 0 votes
Brief: Demonstrates on binding a DataList control to an 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 control to work with factory methods
  • Using a DataList control
  • Adding a simple ItemTemplate to DataList control
  • Binding a DataList control to ObjectDataSource control
  • 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.        <asp:DataList ID="DataList1" runat="server" DataSourceID="ObjectDataSource1" Width="700px">
12.            <ItemTemplate>
13.                Customer
14.                <asp:Label ID="ContactNameLabel" runat="server" Text='<%# Eval("ContactName") %>' />
15.                 with ID
16.                <asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
17.                 works in
18.                <asp:Label ID="CompanyNameLabel" runat="server" Text='<%# Eval("CompanyName") %>' />
19.                <br />
20.                Stays at:
21.                <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
22.                ,
23.                <asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
24.                ,
25.                <asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
26.                <hr />
27.                <br />
28.            </ItemTemplate>
29.        </asp:DataList>
30.        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetList"
31.            TypeName="NorthwindDataAccess.Factories.CustomerFactory"></asp:ObjectDataSource>
32.    </div>
33.    </form>
34.</body>
35.</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