Working with ListView, DataPager and ObjectDataSource

by Jagadish Pulakhandam on 9/20/2011 11:40:08 AM
Rated 0 from 0 votes
Brief: Demonstrates on binding ListView to an ObjectDataSource and paging using DataPager
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 for Paging (with business methods)
  • Configuring ListView to bind to ObjectDataSource control
  • Configuring DataPager to ListView for Paging
  • Adding a simple template to ListView to display data
  • Binding with writing no 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 Order
09.    {
10.        public int OrderID { get; set; }
11.        public string CustomerID { get; set; }
12.        public int EmployeeID { get; set; }
13.        public DateTime OrderDate { get; set; }
14.        public double Freight { get; set; }
15.        public string ShipCity { get; set; }
16.        public string ShipCountry { get; set; }
17.    }
18.}

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 OrderFactory
11.    {
12. 
13.        public static List<Order> GetPagedList(int StartIndex, int PageSize)
14.        {
15.            //It is best to include the following logic in a stored procedure (for great performance)
16.            //This logic is tweaked to have "StartIndex" starting with 0 as the first row index (PageIndex of GridView start at 0)
17.            StringBuilder sb = new StringBuilder();
18.            sb.AppendLine("DECLARE @startRowIndex INT, @maximumRows INT");
19.            sb.AppendLine("SELECT @startRowIndex = " + StartIndex.ToString());
20.            sb.AppendLine("SELECT @maximumRows = " + PageSize.ToString());
21.            sb.AppendLine("IF @startRowIndex = 0");
22.            sb.AppendLine("     SELECT @startRowIndex = 1");
23.            sb.AppendLine("ELSE");
24.            sb.AppendLine("     SELECT @startRowIndex = @startRowIndex + 1");
25.            sb.AppendLine("DECLARE @first_id INT, @startRow INT");
26.            sb.AppendLine("SET ROWCOUNT @startRowIndex");
27.            sb.AppendLine("SELECT @first_id = OrderID FROM Orders ORDER BY OrderID");
28.            sb.AppendLine("SET ROWCOUNT @maximumRows");
29.            sb.AppendLine("SELECT OrderID, CustomerID, EmployeeID, OrderDate, Freight, ShipCity, ShipCountry FROM Orders ");
30.            sb.AppendLine("WHERE OrderID >= @first_id ORDER BY OrderID");
31.            sb.AppendLine("SET ROWCOUNT 0");
32. 
33.            DataTable dtResult = DbHelper.GetResultSet(sb.ToString());
34.            return dtResult
35.                .AsEnumerable()
36.                .Select(dr => new Models.Order()
37.                {
38.                    OrderID = Convert.ToInt16(dr["OrderID"]),
39.                    CustomerID = dr["CustomerID"].ToString(),
40.                    EmployeeID = Convert.ToInt16(dr["EmployeeID"]),
41.                    OrderDate = Convert.ToDateTime(dr["OrderDate"]),
42.                    Freight = Convert.ToDouble(dr["Freight"]),
43.                    ShipCity = dr["ShipCity"].ToString(),
44.                    ShipCountry = dr["ShipCountry"].ToString()
45.                })
46.                .ToList();
47.        }
48. 
49.        public static int GetOrdersCount()
50.        {
51.            return Convert.ToInt16(DbHelper.GetValue("SELECT COUNT(*) FROM Orders"));
52.        }
53. 
54.    }
55.}

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.    <asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
012.        <AlternatingItemTemplate>
013.            <tr style="">
014.                <td>
015.                    <asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>' />
016.                </td>
017.                <td>
018.                    <asp:Label ID="CustomerIDLabel" runat="server"
019.                        Text='<%# Eval("CustomerID") %>' />
020.                </td>
021.                <td>
022.                    <asp:Label ID="EmployeeIDLabel" runat="server"
023.                        Text='<%# Eval("EmployeeID") %>' />
024.                </td>
025.                <td>
026.                    <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("OrderDate") %>' />
027.                </td>
028.                <td>
029.                    <asp:Label ID="FreightLabel" runat="server" Text='<%# Eval("Freight") %>' />
030.                </td>
031.                <td>
032.                    <asp:Label ID="ShipCityLabel" runat="server" Text='<%# Eval("ShipCity") %>' />
033.                </td>
034.                <td>
035.                    <asp:Label ID="ShipCountryLabel" runat="server"
036.                        Text='<%# Eval("ShipCountry") %>' />
037.                </td>
038.            </tr>
039.        </AlternatingItemTemplate>
040.        <EditItemTemplate>
041.            <tr style="">
042.                <td>
043.                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
044.                        Text="Update" />
045.                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
046.                        Text="Cancel" />
047.                </td>
048.                <td>
049.                    <asp:TextBox ID="OrderIDTextBox" runat="server" Text='<%# Bind("OrderID") %>' />
050.                </td>
051.                <td>
052.                    <asp:TextBox ID="CustomerIDTextBox" runat="server"
053.                        Text='<%# Bind("CustomerID") %>' />
054.                </td>
055.                <td>
056.                    <asp:TextBox ID="EmployeeIDTextBox" runat="server"
057.                        Text='<%# Bind("EmployeeID") %>' />
058.                </td>
059.                <td>
060.                    <asp:TextBox ID="OrderDateTextBox" runat="server"
061.                        Text='<%# Bind("OrderDate") %>' />
062.                </td>
063.                <td>
064.                    <asp:TextBox ID="FreightTextBox" runat="server" Text='<%# Bind("Freight") %>' />
065.                </td>
066.                <td>
067.                    <asp:TextBox ID="ShipCityTextBox" runat="server"
068.                        Text='<%# Bind("ShipCity") %>' />
069.                </td>
070.                <td>
071.                    <asp:TextBox ID="ShipCountryTextBox" runat="server"
072.                        Text='<%# Bind("ShipCountry") %>' />
073.                </td>
074.            </tr>
075.        </EditItemTemplate>
076.        <EmptyDataTemplate>
077.            <table runat="server" style="">
078.                <tr>
079.                    <td>
080.                        No data was returned.</td>
081.                </tr>
082.            </table>
083.        </EmptyDataTemplate>
084.        <InsertItemTemplate>
085.            <tr style="">
086.                <td>
087.                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
088.                        Text="Insert" />
089.                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
090.                        Text="Clear" />
091.                </td>
092.                <td>
093.                    <asp:TextBox ID="OrderIDTextBox" runat="server" Text='<%# Bind("OrderID") %>' />
094.                </td>
095.                <td>
096.                    <asp:TextBox ID="CustomerIDTextBox" runat="server"
097.                        Text='<%# Bind("CustomerID") %>' />
098.                </td>
099.                <td>
100.                    <asp:TextBox ID="EmployeeIDTextBox" runat="server"
101.                        Text='<%# Bind("EmployeeID") %>' />
102.                </td>
103.                <td>
104.                    <asp:TextBox ID="OrderDateTextBox" runat="server"
105.                        Text='<%# Bind("OrderDate") %>' />
106.                </td>
107.                <td>
108.                    <asp:TextBox ID="FreightTextBox" runat="server" Text='<%# Bind("Freight") %>' />
109.                </td>
110.                <td>
111.                    <asp:TextBox ID="ShipCityTextBox" runat="server"
112.                        Text='<%# Bind("ShipCity") %>' />
113.                </td>
114.                <td>
115.                    <asp:TextBox ID="ShipCountryTextBox" runat="server"
116.                        Text='<%# Bind("ShipCountry") %>' />
117.                </td>
118.            </tr>
119.        </InsertItemTemplate>
120.        <ItemTemplate>
121.            <tr style="">
122.                <td>
123.                    <asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>' />
124.                </td>
125.                <td>
126.                    <asp:Label ID="CustomerIDLabel" runat="server"
127.                        Text='<%# Eval("CustomerID") %>' />
128.                </td>
129.                <td>
130.                    <asp:Label ID="EmployeeIDLabel" runat="server"
131.                        Text='<%# Eval("EmployeeID") %>' />
132.                </td>
133.                <td>
134.                    <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("OrderDate") %>' />
135.                </td>
136.                <td>
137.                    <asp:Label ID="FreightLabel" runat="server" Text='<%# Eval("Freight") %>' />
138.                </td>
139.                <td>
140.                    <asp:Label ID="ShipCityLabel" runat="server" Text='<%# Eval("ShipCity") %>' />
141.                </td>
142.                <td>
143.                    <asp:Label ID="ShipCountryLabel" runat="server"
144.                        Text='<%# Eval("ShipCountry") %>' />
145.                </td>
146.            </tr>
147.        </ItemTemplate>
148.        <LayoutTemplate>
149.            <table runat="server">
150.                <tr runat="server">
151.                    <td runat="server">
152.                        <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
153.                            <tr runat="server" style="">
154.                                <th runat="server">
155.                                    OrderID</th>
156.                                <th runat="server">
157.                                    CustomerID</th>
158.                                <th runat="server">
159.                                    EmployeeID</th>
160.                                <th runat="server">
161.                                    OrderDate</th>
162.                                <th runat="server">
163.                                    Freight</th>
164.                                <th runat="server">
165.                                    ShipCity</th>
166.                                <th runat="server">
167.                                    ShipCountry</th>
168.                            </tr>
169.                            <tr ID="itemPlaceholder" runat="server">
170.                            </tr>
171.                        </table>
172.                    </td>
173.                </tr>
174.                <tr runat="server">
175.                    <td runat="server" style="">
176.                    </td>
177.                </tr>
178.            </table>
179.        </LayoutTemplate>
180.        <SelectedItemTemplate>
181.            <tr style="">
182.                <td>
183.                    <asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>' />
184.                </td>
185.                <td>
186.                    <asp:Label ID="CustomerIDLabel" runat="server"
187.                        Text='<%# Eval("CustomerID") %>' />
188.                </td>
189.                <td>
190.                    <asp:Label ID="EmployeeIDLabel" runat="server"
191.                        Text='<%# Eval("EmployeeID") %>' />
192.                </td>
193.                <td>
194.                    <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("OrderDate") %>' />
195.                </td>
196.                <td>
197.                    <asp:Label ID="FreightLabel" runat="server" Text='<%# Eval("Freight") %>' />
198.                </td>
199.                <td>
200.                    <asp:Label ID="ShipCityLabel" runat="server" Text='<%# Eval("ShipCity") %>' />
201.                </td>
202.                <td>
203.                    <asp:Label ID="ShipCountryLabel" runat="server"
204.                        Text='<%# Eval("ShipCountry") %>' />
205.                </td>
206.            </tr>
207.        </SelectedItemTemplate>
208.    </asp:ListView>
209.    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1"
210.        PageSize="20">
211.        <Fields>
212.            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
213.                ShowLastPageButton="True" />
214.        </Fields>
215.    </asp:DataPager>
216.    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnablePaging="True"
217.        MaximumRowsParameterName="PageSize" SelectCountMethod="GetOrdersCount"
218.        SelectMethod="GetPagedList" StartRowIndexParameterName="StartIndex"
219.        TypeName="NorthwindDataAccess.Factories.OrderFactory">
220.    </asp:ObjectDataSource>
221.    <div>
222.     
223.    </div>
224.    </form>
225.</body>
226.</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