Binding ListView to ObjectDataSource

by Jagadish Pulakhandam on 9/20/2011 11:30:58 AM
Rated 0 from 0 votes
Brief: Demonstrates on binding a ListView 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 control to work with factory methods
  • Using ListView control
  • Adding a simple ItemTemplate to ListView control
  • Binding a ListView 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.}

001.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NorthwindWebSite.WebForm1" %>
002. 
003.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
004. 
005.<html xmlns="http://www.w3.org/1999/xhtml">
006.<head runat="server">
007.    <title></title>
008.</head>
009.<body>
010.    <form id="form1" runat="server">
011.    <div>
012.     
013.        <asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1"
014.            GroupItemCount="3">
015.            <AlternatingItemTemplate>
016.                <td runat="server" style="background-color: #FFFFFF;color: #284775;">
017.                    <asp:Label ID="ContactNameLabel" runat="server" Font-Bold="True"
018.                        Font-Size="12px" Text='<%# Eval("ContactName") %>'></asp:Label>
019.                    <asp:Label ID="CustomerIDLabel" runat="server" Font-Size="8px"
020.                        Text='<%# Eval("CustomerID") %>' />
021.                    <br />
022.                    <asp:Label ID="CompanyNameLabel" runat="server" Font-Size="12px"
023.                        Text='<%# Eval("CompanyName") %>' />
024.                    <br />
025.                    <asp:Label ID="AddressLabel" runat="server" Font-Size="12px"
026.                        Text='<%# Eval("Address") %>' />
027.                    <br />
028.                    <asp:Label ID="CityLabel" runat="server" Font-Size="12px"
029.                        Text='<%# Eval("City") %>' />
030.                    <br />
031.                    <asp:Label ID="CountryLabel" runat="server" Font-Size="12px"
032.                        Text='<%# Eval("Country") %>' />
033.                    <br />
034.                </td>
035.            </AlternatingItemTemplate>
036.            <EditItemTemplate>
037.                <td runat="server" style="background-color: #999999;">
038.                    CustomerID:
039.                    <asp:TextBox ID="CustomerIDTextBox" runat="server"
040.                        Text='<%# Bind("CustomerID") %>' />
041.                    <br />CompanyName:
042.                    <asp:TextBox ID="CompanyNameTextBox" runat="server"
043.                        Text='<%# Bind("CompanyName") %>' />
044.                    <br />ContactName:
045.                    <asp:TextBox ID="ContactNameTextBox" runat="server"
046.                        Text='<%# Bind("ContactName") %>' />
047.                    <br />Address:
048.                    <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' />
049.                    <br />City:
050.                    <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
051.                    <br />Country:
052.                    <asp:TextBox ID="CountryTextBox" runat="server" Text='<%# Bind("Country") %>' />
053.                    <br />
054.                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
055.                        Text="Update" />
056.                    <br />
057.                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
058.                        Text="Cancel" />
059.                    <br />
060.                </td>
061.            </EditItemTemplate>
062.            <EmptyDataTemplate>
063.                <table runat="server"
064.                    style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
065.                    <tr>
066.                        <td>
067.                            No data was returned.</td>
068.                    </tr>
069.                </table>
070.            </EmptyDataTemplate>
071.            <EmptyItemTemplate>
072.<td runat="server" />
073.            </EmptyItemTemplate>
074.            <GroupTemplate>
075.                <tr ID="itemPlaceholderContainer" runat="server">
076.                    <td ID="itemPlaceholder" runat="server">
077.                    </td>
078.                </tr>
079.            </GroupTemplate>
080.            <InsertItemTemplate>
081.                <td runat="server" style="">
082.                    CustomerID:
083.                    <asp:TextBox ID="CustomerIDTextBox" runat="server"
084.                        Text='<%# Bind("CustomerID") %>' />
085.                    <br />CompanyName:
086.                    <asp:TextBox ID="CompanyNameTextBox" runat="server"
087.                        Text='<%# Bind("CompanyName") %>' />
088.                    <br />ContactName:
089.                    <asp:TextBox ID="ContactNameTextBox" runat="server"
090.                        Text='<%# Bind("ContactName") %>' />
091.                    <br />Address:
092.                    <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' />
093.                    <br />City:
094.                    <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
095.                    <br />Country:
096.                    <asp:TextBox ID="CountryTextBox" runat="server" Text='<%# Bind("Country") %>' />
097.                    <br />
098.                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
099.                        Text="Insert" />
100.                    <br />
101.                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
102.                        Text="Clear" />
103.                    <br />
104.                </td>
105.            </InsertItemTemplate>
106.            <ItemTemplate>
107.                <td runat="server" style="background-color: #E0FFFF;color: #333333;">
108.                    <asp:Label ID="ContactNameLabel" runat="server" Font-Bold="True"
109.                        Font-Size="12px" Text='<%# Eval("ContactName") %>'></asp:Label>
110.                    <asp:Label ID="CustomerIDLabel" runat="server" Font-Size="8px"
111.                        Text='<%# Eval("CustomerID") %>' />
112.                    <br />
113.                    <asp:Label ID="CompanyNameLabel" runat="server" Font-Size="12px"
114.                        Text='<%# Eval("CompanyName") %>' />
115.                    <br />
116.                    <asp:Label ID="AddressLabel" runat="server" Font-Size="12px"
117.                        Text='<%# Eval("Address") %>' />
118.                    <br />
119.                    <asp:Label ID="CityLabel" runat="server" Font-Size="12px"
120.                        Text='<%# Eval("City") %>' />
121.                    <br />
122.                    <asp:Label ID="CountryLabel" runat="server" Font-Size="12px"
123.                        Text='<%# Eval("Country") %>' />
124.                    <br />
125.                </td>
126.            </ItemTemplate>
127.            <LayoutTemplate>
128.                <table runat="server">
129.                    <tr runat="server">
130.                        <td runat="server">
131.                            <table ID="groupPlaceholderContainer" runat="server" border="1"
132.                                style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
133.                                <tr ID="groupPlaceholder" runat="server">
134.                                </tr>
135.                            </table>
136.                        </td>
137.                    </tr>
138.                    <tr runat="server">
139.                        <td runat="server"
140.                            style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
141.                        </td>
142.                    </tr>
143.                </table>
144.            </LayoutTemplate>
145.            <SelectedItemTemplate>
146.                <td runat="server"
147.                    style="background-color: #E2DED6;font-weight: bold;color: #333333;">
148.                    CustomerID:
149.                    <asp:Label ID="CustomerIDLabel" runat="server"
150.                        Text='<%# Eval("CustomerID") %>' />
151.                    <br />CompanyName:
152.                    <asp:Label ID="CompanyNameLabel" runat="server"
153.                        Text='<%# Eval("CompanyName") %>' />
154.                    <br />ContactName:
155.                    <asp:Label ID="ContactNameLabel" runat="server"
156.                        Text='<%# Eval("ContactName") %>' />
157.                    <br />Address:
158.                    <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
159.                    <br />City:
160.                    <asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
161.                    <br />Country:
162.                    <asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
163.                    <br />
164.                </td>
165.            </SelectedItemTemplate>
166.        </asp:ListView>
167.        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
168.            SelectMethod="GetList" TypeName="NorthwindDataAccess.Factories.CustomerFactory">
169.        </asp:ObjectDataSource>
170.     
171.    </div>
172.    </form>
173.</body>
174.</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