Castle+IBatis.Net分页

Castle+IBatis.Net分页

<整理:sure_huang 2008-09-23>

      最近学习Castle+IBatisNet,分页的问题困扰我很久,在网上也有一些解决IBatisNet分页的思路,但却没有详细的代码,给初学者带来不便。现在把详细代码贴出,跟大家分享!

一.项目框架参考NPetShop,整个解决方案分为
Domain -- 业务实体层
Persistence -- 持久层,负责数据访问
Service -- 服务层,调用持久层方法并实现业务逻辑
Web -- 最终展现给客户的Web界面,调用Service层方法
Componet -- 主要包括一些通用的方法等

二.Persistence:(BaseSqlMapDao类)

     1.BaseSqlMapDao.cs  
       

ContractedBlock.gif ExpandedBlockStart.gif Code
 1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 2        /// 分页方法,得到分页的列表
 3        /// </summary>
 4        /// <param name="statementName">操作名称</param>
 5        /// <param name="parameterObject">参数</param>
 6        /// <param name="pageSize">每页记录数</param>
 7        /// <returns></returns>

 8        protected IPaginatedList ExecuteQueryForPaginatedList(string statementName, object parameterObject, int pageSize)
 9ExpandedBlockStart.gifContractedBlock.gif        {
10            try
11ExpandedSubBlockStart.gifContractedSubBlock.gif            {
12                return sqlMap.QueryForPaginatedList(statementName, parameterObject, pageSize);
13            }

14            catch (Exception e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif            {
16                throw new DataMapperException("Error executing query '" + statementName + "' for paginated list. Cause: " + e.Message, e);
17            }

18        }

19


2.UsersDB.cs
 

ContractedBlock.gif ExpandedBlockStart.gif Code
1ContractedBlock.gifExpandedBlockStart.gif用于分页#region 用于分页
2        public IPaginatedList GetUserPagerList(int PageSize)
3ExpandedSubBlockStart.gifContractedSubBlock.gif        {
4            return ExecuteQueryForPaginatedList("SelectUsers"null, PageSize);
5        }

6        #endregion

7

      

三.Component(略)

四.Model(略)

五.Service:

1.IUsersService.cs(IUsersService接口)

 

ContractedBlock.gif ExpandedBlockStart.gif Code
1ContractedBlock.gifExpandedBlockStart.gif       用于分页#region 用于分页
2        IPaginatedList GetUserPagerList(int PageSize);
3        #endregion

4


 2.UsersService.cs(UsersService类,继承IUsersService接口)
 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1       private UsersDB _users;
 2        public UsersService(UsersDB users)
 3ExpandedBlockStart.gifContractedBlock.gif        {
 4            _users = users;
 5        }

 6ContractedBlock.gifExpandedBlockStart.gif        用于分页#region 用于分页
 7        public IPaginatedList GetUserPagerList(int PageSize)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 9            return _users.GetUserPagerList(PageSize);
10        }

11        #endregion

 

六.Web(UI层)

1.***.aspx:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1<asp:GridView ID="GridView1" runat="server" Width="580px" 
 2            AutoGenerateColumns="False" DataKeyNames="users_ID" 
 3            onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting">
 4            <Columns>
 5                <asp:BoundField HeaderText="No." />
 6                <asp:HyperLinkField DataNavigateUrlFields="users_ID" 
 7                    DataNavigateUrlFormatString="UserInfo.aspx?users_ID={0}" 
 8                    DataTextField="users_ID" HeaderText="用户ID" Target="_blank" />
 9                <asp:BoundField DataField="users_Name" HeaderText="用户名" />
10                <asp:TemplateField HeaderText="性别">
11                    <EditItemTemplate>
12                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("users_Sex") %>'></asp:TextBox>
13                    </EditItemTemplate>
14                     <ItemTemplate>
15                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("users_Sex").ToString()=="0"?"女":"男" %>'></asp:Label>
16                    </ItemTemplate>
17                </asp:TemplateField>
18                <asp:BoundField DataField="users_Add" HeaderText="地址" />
19                <asp:BoundField DataField="users_Email" HeaderText="Email" />
20                <asp:BoundField DataField="users_DateTime" HeaderText="开户时间" />
21                <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
22            </Columns>
23        </asp:GridView>
24<table style="width:580px;">
25                       <tr>
26                           <td align="left">
27                               <asp:LinkButton ID="lbFrist" runat="server" CommandName="first" 
28                                   oncommand="GridViewPaging">|&lt;Frist</asp:LinkButton>
29                               <asp:LinkButton ID="LinkbuttonPrev" runat="server" CommandName="prev" 
30                                   oncommand="GridViewPaging">&lt;&lt;Prev</asp:LinkButton>
31                           </td>
32                           <td>
33                               <asp:Label ID="Label16" runat="server" Text="Page:"></asp:Label>
34                               <asp:Label ID="lbCurrentPage" Style="text-align:left;" runat="server"></asp:Label>
35                               <asp:Label ID="Label17" runat="server" Text="/"></asp:Label>
36                               <asp:Label ID="lbPageCount" Style="text-align:left;" runat="server"></asp:Label>
37                               <asp:Label ID="Label10" runat="server" Text="Totle:"></asp:Label>
38                               <asp:Label ID="lbRecordCount" Style="text-align:left;" runat="server"></asp:Label>
39                           </td>
40                           <td align="right">
41                               <asp:LinkButton ID="LinkbuttonNext" runat="server" CommandName="next" 
42                                   oncommand="GridViewPaging">Next&gt;&gt;</asp:LinkButton>
43                               <asp:LinkButton ID="lbLast" runat="server" CommandName="last" 
44                                   oncommand="GridViewPaging">Last&gt;|</asp:LinkButton>
45                           </td>
46                       </tr>
47                   </table>
48
49

 

2.***.aspx.cs:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1ContractedBlock.gifExpandedBlockStart.gif定义分页变量#region 定义分页变量
 2        int CurrentPage;//当前页数 
 3        int PageSize=10;   //每页条数 
 4        int PageCount; //总页数 
 5        int RecordCount;//总条数 
 6        #endregion

 7
 8        private IUsersService _org;
 9        public IUsersService org
10ExpandedBlockStart.gifContractedBlock.gif        {
11ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _org = value; }
12        }

13ContractedBlock.gifExpandedBlockStart.gifPage_Load事件#region Page_Load事件
14        protected void Page_Load(object sender, EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        {
16            if (!Page.IsPostBack)
17ExpandedSubBlockStart.gifContractedSubBlock.gif            {
18ContractedSubBlock.gifExpandedSubBlockStart.gif                分页所用#region 分页所用
19                CurrentPage = 0;//当前页习惯设为0 
20                ViewState["PageIndex"= 0;//页索引也设为0 
21                RecordCount = _org.GetUserPageCount().PageCount;//总记录数
22                if (RecordCount % PageSize == 0)//计算总共有多少页 
23ExpandedSubBlockStart.gifContractedSubBlock.gif                {
24                    PageCount = RecordCount / PageSize;
25                }

26                else
27ExpandedSubBlockStart.gifContractedSubBlock.gif                {
28                    PageCount = RecordCount / PageSize + 1;
29                }

30                ViewState["PageCount"= PageCount;//会话session对整个application有效,而视图状态viewstate相当于某个页面的session 
31                this.lbRecordCount.Text = RecordCount.ToString();//显示总记录数
32                this.lbPageCount.Text = PageCount.ToString();//显示总页数
33                #endregion

34
35                GridViewBind();
36            }

37        }

38        #endregion

39ContractedBlock.gifExpandedBlockStart.gifGridView绑定#region GridView绑定
40        private void GridViewBind()
41ExpandedSubBlockStart.gifContractedSubBlock.gif        {
42            int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
43
44            IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
45            this.GridView1.DataSource = pagerlist;
46            this.GridView1.DataBind();
47
48            this.LinkbuttonPrev.Enabled = true;
49            this.LinkbuttonNext.Enabled = true;
50            
51            if (CurrentPage == (PageCount - 1)) this.LinkbuttonNext.Enabled = false;//当为最后一页时,下一页链接按钮不可用 
52            if (CurrentPage == 0this.LinkbuttonPrev.Enabled = false;//当为第一页时,上一页按钮不可用 
53            this.lbCurrentPage.Text = (CurrentPage + 1).ToString();//当前页数
54        }

55        #endregion

56ContractedBlock.gifExpandedBlockStart.gif分页事件#region 分页事件
57        protected void GridViewPaging(object sender, CommandEventArgs e)
58ExpandedSubBlockStart.gifContractedSubBlock.gif        {
59            IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
60            pagerlist.NextPage();
61
62            CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引 
63            PageCount = (int)ViewState["PageCount"];//获得总页数 
64
65            string cmd = e.CommandName;
66            //判断cmd,以判定翻页方向 
67            switch (cmd)
68ExpandedSubBlockStart.gifContractedSubBlock.gif            {
69                case "prev"://上一页 
70                    if (CurrentPage > 0) CurrentPage--;
71                    break;
72
73                case "next":
74                    if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页 
75                    break;
76
77                case "first"://第一页 
78                    CurrentPage = 0;
79                    break;
80
81                case "last"://最后一页 
82                    CurrentPage = PageCount - 1;
83                    break;
84            }

85            pagerlist.GotoPage(CurrentPage);//页面跳转
86            ViewState["PageIndex"= CurrentPage;//获得当前页 
87            GridViewBind(); 
88            this.GridView1.DataSource = pagerlist;
89            this.GridView1.DataBind();
90        }

91        #endregion

92

 

七.效果图:

转载于:https://www.cnblogs.com/sure_huang/archive/2008/11/16/1334475.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值