using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Collections.Generic; 
using Model; 
using BLL; 
namespace ShoutoutWallTest 
{ 
public partial class AllShoutout : System.Web.UI.Page 
{ 
public static List<Shoutout> list = null; 
private static int ItemCount = 0; 
protected void Page_Load(object sender, EventArgs e) 
{ 
} 
public List<Shoutout> LoadShoutouts(int startIndex, int maxRows) 
{ 
int itemCount; 
int pageIndex = 1; 
if (startIndex > 0) 
{ 
pageIndex = (startIndex) / 25 + 1; 
} 
list = ShoutoutBLL.GetShoutouts(13, pageIndex, maxRows, true, out itemCount); 
ItemCount = itemCount; 
return list; 
} 
public int CountAll() 
{ 
return ItemCount; 
} 
/// <summary> 
/// Refresh data after updating and deleting. 
/// </summary> 
private void RefreshData() 
{ 
lvShoutout.DataSourceID = ObjectDataSource1.ID; 
} 
} 
} 
我的代码中要求每页显示25条数据,ShoutoutBLL.GetShoutouts()方法有5个参数,第一个参数用于指定检索数据的条件,这个是程序中的特例,读者可以不用关心;第二个参数是页面的索引,规定从1开始,我在方法中从startIndex转换成了pageIndex;第三个参数是每页显示的数据条数;第四个参数是out类型的,返回记录总行数,这个方法主要是为了对应执行数据库的存储过程,具体代码在BLL命名空间中,属于业务逻辑层的代码,这里就不再具体给出了,Model命名空间中的代码主要用来返回数据库实体对象,如Shoutout和Image对象。RefreshData()方法中重新给ListView控件的DataSourceID属性指定了值,这样可以重新绑定数据从而达到刷新数据的效果。

复制代码 代码如下:
 
set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
go 
ALTER PROCEDURE [dbo].[GetShoutOuts] 
-- Add the parameters for the stored procedure here 
( 
@LocationID INT, 
@PageIndex INT, -- start from 1. 
@PageSize INT, 
@showimages BIT, 
@ItemCount INT Output 
) 
AS 
BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 
declare @beginRowNumber bigint,@endRowNumber bigint 
set @beginRowNumber = (@PageIndex - 1)*@PageSize+1; 
set @endRowNumber = @PageIndex*@PageSize; 
WITH TempPagingRecord AS 
( 
SELECT ROW_NUMBER() OVER(ORDER BY PostedDate DESC) AS RecordNumber,SO.ID AS ShoutOutID, BC.[Description], BC.PostedByName, BC.PostedDate,null as ImageTitle,null as ImageBlob,null as Type,BC.ID AS BaseCommentID,BC.DisplayUserName, 
BC.IsVisible, SO.NotifyToShoutOutUser, SO.ShoutOutToUserAlias 
FROM dbo.ShoutOut as SO 
JOIN dbo.BaseComment as BC ON BC.ID = SO.BaseCommentID 
WHERE SO.LocationID =@LocationID 
AND BC.IsVisible = 1 
) 
SELECT RecordNumber, 
ShoutOutID, 
Description, 
PostedByName, 
PostedDate, 
ImageTitle, 
ImageBlob, 
BaseCommentID, 
DisplayUserName, 
IsVisible, 
NotifyToShoutOutUser, 
ShoutOutToUserAlias 
INTO #tempTable 
FROM TempPagingRecord 
Where RecordNumber between @beginRowNumber and @endRowNumber 
-- Insert statements for procedure here 
IF(@showimages = 1) 
begin 
select RecordNumber, 
ShoutOutID, 
Description, 
PostedByName, 
PostedDate, 
IM.ImageTitle, 
IM.ImageBlob, 
IM.Type, 
T.BaseCommentID, 
DisplayUserName, 
IsVisible, 
NotifyToShoutOutUser, 
ShoutOutToUserAlias from #tempTable T 
Left join dbo.Image IM ON IM.BaseCommentID = T.BaseCommentID 
order by PostedDate DESC 
end 
ELSE 
begin 
SELECT * FROM #tempTable 
order by PostedDate DESC 
end 
SELECT @ItemCount = Count(*) 
FROM Shoutout as SO 
JOIN dbo.BaseComment as BC ON BC.ID = SO.BaseCommentID 
WHERE SO.LocationID =@LocationID 
AND BC.IsVisible = 1 
END 
个人觉得ObjectDataSource控件是一个比较智能化的控件,它通过函数委托的方式自动执行用户提供的分页代码来完成数据库的“真分页”操作,省去了开发过程中的很多麻烦,还是很有必要去认真研究一下的。
您可能感兴趣的文章:
