Rainbow分页解决方案

本文介绍如何为Rainbow门户项目的Announcements模块添加分页功能,包括代码修改、存储过程创建及数据访问层调整等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rainbow是C#编写的一个优秀的开源门户网站项目。无论是实际应用还是拿来学习,都是一个不错的选择。目前似乎汉化最好的是RC4的版本,在bussoft有下载。但遗憾的是其中很多的功能模块都不提供分页的功能。在网上找了下,发现以下的解决方案:
Rainbow其实已经自带分页的功能支持,如在Rainbow.UI.WebControls下,IPaging接口、Paging和PagingNumbers控件在RC4版本中都是现成的了。
在每个对应模块中按照一定的步骤适当修改一下原有的代码及其存储过程,就可以达到分页的目的:
以Announcements模块为例:
1. 增加代码到ascx的头部分:

None.gif<%@ Register TagPrefix="cc2" Namespace="Rainbow.UI.WebControls" Assembly="Rainbow" %>

在需要放置分页器的地方放置以下代码:

None.gif<p align =right ><cc2:Paging id="pgModules" runat="server" /></p>

这个是放置在右下角的例子。
2. 在ascx对应的cs代码中声明分页器模块:

None.gifprotected Rainbow.UI.WebControls.IPaging pgModules;

3. 声明分页的缺省大小:

None.gif            SettingItem PageSize = new SettingItem(new 
None.gif
None.gifRainbow.UI.DataTypes.IntegerDataType());
None.gif            PageSize.Value = "10";
None.gif            this._baseSettings.Add("PageSize",PageSize);
None.gif

放在构造函数Announcement()中。

4. 在初始化OnInit代码部分添加事件代理的响应:

None.gifpgModules.OnMove += new EventHandler(Page_Changed);

5. 当然需要添加对应的Page_Changed代码:

None.gif  private void Page_Changed(object sender, System.EventArgs e)
ExpandedBlockStart.gif  {
InBlock.gif    pgModules.RecordsPerPage = Int32.Parse(Settings["PageSize"].ToString());
InBlock.gif    BindList(pgModules.PageNumber);  
ExpandedBlockEnd.gif  }
None.gif

6. 定义新的DataBind函数:

None.gif        private void BindList(int Page)
ExpandedBlockStart.gif        {
InBlock.gif            string sortField = Settings["SortField"].ToString();
InBlock.gif            string sortDirection = Settings["SortDirection"].ToString();
InBlock.gif
InBlock.gif            AnnouncementsDB announcements = new AnnouncementsDB();
InBlock.gif            DataSet announces = announcements.GetAnnouncementsPaged(ModuleID, 
InBlock.gif
InBlock.gifVersion,Page,pgModules.RecordsPerPage);
InBlock.gif            
InBlock.gif            if (announces.Tables.Count>0 && announces.Tables[0].Rows.Count >0)
ExpandedSubBlockStart.gif            {
InBlock.gif               pgModules.RecordCount = (int)(announces.Tables[0].Rows[0]["RecordCount"]);
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif                DataView myDataView = new DataView();
InBlock.gif                myDataView = announces.Tables[0].DefaultView;
InBlock.gif                myDataView.Sort = sortField + " " + sortDirection;            
InBlock.gif                myDataList.DataSource = myDataView;
InBlock.gif                myDataList.DataBind();
ExpandedBlockEnd.gif        }
None.gif

7. 在Page_Load中,替换掉原来的Databind的方法:

None.gif                pgModules.RecordsPerPage = Int32.Parse(Settings["PageSize"].ToString());
None.gif                BindList(pgModules.PageNumber);None.gif

8. 在数据库中建立对应的分页操作的存储过程,如 rb_GetAnnouncementsPaged,注意付给适当的权限。

None.gifCREATE   PROCEDURE rb_GetAnnouncementsPaged
None.gif(
None.gif    @ModuleID int,
None.gif    @WorkflowVersion int,
None.gif    @Page int,
None.gif    @RecordsPerPage int
None.gif)
None.gifAS
None.gif
None.gifNone.gifDECLARE @FirstRec int, @LastRec int
None.gif
None.gifNone.gifCREATE TABLE #TempItems
None.gif(
None.gifID int IDENTITY,
None.gifItemID int,
None.gifCreatedByUser nvarchar(100),
None.gifCreatedDate datetime,
None.gifTitle nvarchar(150),
None.gifMoreLink nvarchar(150),
None.gifMobileMoreLink nvarchar(150),
None.gifExpireDate datetime,
None.gifDescription nvarchar(2000)
None.gif)
None.gif
None.gifBEGIN 
None.gifSET NOCOUNT ON
None.gif
None.gifIF ( @WorkflowVersion = 1 )
None.gifINSERT INTO
None.gif#TempItems
None.gif(
None.gifItemID, CreatedByUser, CreatedDate, Title,
None.gifMoreLink, MobileMoreLink, ExpireDate, Description
None.gif)
None.gif    SELECT
None.gif        ItemID,
None.gif        CreatedByUser,
None.gif        CreatedDate,
None.gif        Title,
None.gif        MoreLink,
None.gif        MobileMoreLink,
None.gif        ExpireDate,
None.gif        Description
None.gif    FROM 
None.gif        rb_Announcements
None.gif    WHERE
None.gif        ModuleID = @ModuleID
None.gif      AND
None.gif        ExpireDate > GETDATE()
None.gif    ORDER BY CreatedDate DESC
None.gifELSE
None.gifINSERT INTO
None.gif#TempItems
None.gif(
None.gifItemID, CreatedByUser, CreatedDate, Title,
None.gifMoreLink, MobileMoreLink, ExpireDate, Description
None.gif)
None.gif    SELECT
None.gif        ItemID,
None.gif        CreatedByUser,
None.gif        CreatedDate,
None.gif        Title,
None.gif        MoreLink,
None.gif        MobileMoreLink,
None.gif        ExpireDate,
None.gif        Description
None.gif    FROM 
None.gif        rb_Announcements_st
None.gif    WHERE
None.gif        ModuleID = @ModuleID
None.gif      AND
None.gif        ExpireDate > GETDATE()
None.gif    ORDER BY CreatedDate DESC
None.gif
None.gifSELECT @FirstRec = (@Page - 1* @RecordsPerPage
None.gifSELECT @LastRec = (@Page * @RecordsPerPage + 1)
None.gif
None.gifSELECT *, (SELECT COUNT(*FROM #TempItems) RecordCount
None.gifFROM #TempItems
None.gifWHERE ID>@FirstRec AND ID<@LastRec
None.gif
None.gifSET NOCOUNT OFF
None.gifEND
None.gif
None.gifGO
None.gif

9. 当然,数据访问层也需要修改,如6中也可以看到了,需要增加GetAnnouncementsPaged函数在AnnouncementsDB中:

None.gif        public DataSet GetAnnouncementsPaged(int moduleID, WorkFlowVersion version,int page, 
None.gif
None.gifint perPageNumber) 
ExpandedBlockStart.gif        {
InBlock.gif            SqlConnection myConnection = PortalSettings.SqlConnectionString;
InBlock.gif            SqlDataAdapter myCommand = new SqlDataAdapter("rb_GetAnnouncementsPaged", myConnection);
InBlock.gif            myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
InBlock.gif            SqlParameter parameterModuleID = new SqlParameter("@ModuleID", SqlDbType.Int, 4);
InBlock.gif            parameterModuleID.Value = moduleID;
InBlock.gif            myCommand.SelectCommand.Parameters.Add(parameterModuleID);
InBlock.gif            SqlParameter parameterWorkflowVersion = new SqlParameter("@WorkflowVersion", SqlDbType.Int, 4);
InBlock.gif            parameterWorkflowVersion.Value = (int)version;
InBlock.gif            myCommand.SelectCommand.Parameters.Add(parameterWorkflowVersion);
InBlock.gif            SqlParameter parameterPage = new SqlParameter("@Page", SqlDbType.Int, 4);
InBlock.gif            parameterPage.Value = page;
InBlock.gif            myCommand.SelectCommand.Parameters.Add(parameterPage);
InBlock.gif            SqlParameter parameterPerPageNumber = new SqlParameter("@RecordsPerPage", SqlDbType.Int, 4);
InBlock.gif            parameterPerPageNumber.Value = perPageNumber;
InBlock.gif            myCommand.SelectCommand.Parameters.Add(parameterPerPageNumber);
InBlock.gif            DataSet myDataSet = new DataSet();
InBlock.gif            try
ExpandedSubBlockStart.gif            {
InBlock.gif                myCommand.Fill(myDataSet);
ExpandedSubBlockEnd.gif            }
InBlock.gif            finally
ExpandedSubBlockStart.gif            {
InBlock.gif                myConnection.Close(); 
InBlock.gif             }
InBlock.gif            return myDataSet;
ExpandedBlockEnd.gif        }
None.gif

10. 有些模块原来是使用SqlDataReader的,在分页中也可以适当将其分页用DataSet来替代,如Discussion模块。


本文转自风前絮~~博客园博客,原文链接:http://www.cnblogs.com/windsails/archive/2004/12/16/77953.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值