GridView控件自定义分页详解
前些天我写了关于 <<在存储过程中实现分页>>和<<GridView控件事件详解 >> ,后来又有一些人问我怎样在GridView中应用这个东东!其实很简单,主要是怎么保存当前页面的页码PageIndex问题,不过把这个解决了什么都好办了.因为在分页过程中:PageSize是一定的,我们可以用一个属性来表示.保存PageIndex好多中方法,而且数据不是很庞大,基本不会好太多的资源.还是一句老话,话再多都没有例子直观.
在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!
是不是很简单啊,看了代码就都懂了,关于用按钮传参数在这里就不多说了,还有一个就是Entities属性,在<<应用实体类EntitySet实现类似的范型功能 >>这篇文章中可以找到!
在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!
1
<asp:GridView ID="NewsGrid" runat="server" AutoGenerateColumns="False" AllowPaging="false" Width="100%">
2
<Columns>
3
<asp:BoundField DataField="NewsId" HeaderText="新闻ID"/>
4
<asp:HyperLinkField DataNavigateUrlFields="NewsId" DataNavigateUrlFormatString="~/Details.aspx?ID={0}"
5
DataTextField="Title" HeaderText="新闻标题" ItemStyle-Width="70%"/>
6
<asp:BoundField DataField="PostTime" HeaderText="发布时间"/>
7
<asp:CommandField HeaderText="新闻管理" ShowCancelButton="False" ShowDeleteButton="True"
8
ShowEditButton="True"/>
9
</Columns>
10
</asp:GridView>
11
<div style=" height:16px; padding-top:5px; margin-right:30px; float:right">
12
<asp:HiddenField ID="CurrentPage" runat="server" Value="0"/>
13
<asp:LinkButton ID="First" runat="server" CommandArgument="first" OnClick="PagerButton_Click">首 页</asp:LinkButton>
14
<asp:LinkButton ID="Prev" runat="server" CommandArgument="prev" OnClick="PagerButton_Click">上一页</asp:LinkButton>
15
<asp:LinkButton ID="Next" runat="server" CommandArgument="next" OnClick="PagerButton_Click">下一页</asp:LinkButton>
16
<asp:LinkButton ID="Last" runat="server" CommandArgument="last" OnClick="PagerButton_Click">尾 页</asp:LinkButton>
17
</div>
CS文件中的代码:![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
1
protected void PagerButton_Click(object sender, EventArgs e)
2
{
3
int pageIndx = Convert.ToInt32(CurrentPage.Value);
4
int totals = NewsManager.GetNews(0, pageSize).TotalRecords;
5
int pages = (totals % pageSize) == 0 ? (totals / pageSize) : (totals / pageSize + 1);
6
string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
7
switch (arg)
8
{
9
case "prev":
10
if (pageIndx > 0)
11
{
12
pageIndx -= 1;
13
}
14
break;
15
case "next":
16
if (pageIndx < pages - 1)
17
{
18
pageIndx += 1;
19
}
20
break;
21
case "last":
22
pageIndx = pages - 1;
23
break;
24
default:
25
pageIndx = 0;
26
break;
27
}
28
CurrentPage.Value = pageIndx.ToString();
29
NewsGrid.DataSource = NewsManager.GetNews(pageIndx , pageSize).Entities;
30
NewsGrid.DataBind();
31
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
是不是很简单啊,看了代码就都懂了,关于用按钮传参数在这里就不多说了,还有一个就是Entities属性,在<<应用实体类EntitySet实现类似的范型功能 >>这篇文章中可以找到!
应用实体类EntitySet实现类似的范型功能
在制做项目时我们都会将数据库中的字段名写成一个类,也就是将数据库中的每一张表写成一个类,我们将它称为实体类.EntitySet类能构将类的很多个实例对象封装在Entities属性中,同时可以记录查询记录的总数,对数据库分页很有用;拥有类是List<>范型功能,应用于所有实体类,能减少我们很多代码.
通常我们要为一个实体类定义四个重要的函数,当然这是最少的要求:(假设我们有一个实体类EntitySimple) 下面我们就定义四个和数据库相关联的函数:
EntitySet其实就是一个ArrayList的变形版,有了这个类在读取是就可以将对象entitySimple给Add进去了,当我们要用的时候就可以直接调用了:EntitySet set=SimpleManager.GetAllEntitySimple().Entities;如果直接使用SimpleManager.GetAllEntitySimple().Entities前显式转化:(EntitySimple)SimpleManager.GetAllEntitySimple().Entities.
代码很简单,但能构充当十分重要的角色,对于一个网站来说需要很多个实体类集合,这里我们就可以用这个类来替代集合类.能构减少很多代码,当然你可以用List<EntitySimple>,但是有时需要TotalRecords,就不能直接从中读取,像上一篇文章中就不需要加out int totalRecords了,可以直接存在这个类里,我觉得这个类会比较好用,所以就拿出来和大家分享一下,或许你有更好的,希望批评指正!
通常我们要为一个实体类定义四个重要的函数,当然这是最少的要求:(假设我们有一个实体类EntitySimple) 下面我们就定义四个和数据库相关联的函数:
1
using System;
2
using System.Data;
3
using System.Data.SqlClient;
4
//其中EntitySimple类位于下面的命名空间中.
5
using NexPlus.Entities;
6![](/Images/OutliningIndicators/None.gif)
7
namespace NexPlus.Data.SqlDataProvider{
8
public class SimpleDataProvider{
9
10
public SimpleDataProvider(){}
11![](/Images/OutliningIndicators/InBlock.gif)
12
public bool CreateUpdateEntitySimple(EntitySimple entitySimple,DataAction action){
13
//代码
.
14
}
15![](/Images/OutliningIndicators/InBlock.gif)
16
public EntitySimple GetEntitySimple(int id){
17
//代码
.
18
}
19![](/Images/OutliningIndicators/InBlock.gif)
20
public bool DeleteEntitySimple(int id){
21
//代码
.
22
}
23![](/Images/OutliningIndicators/InBlock.gif)
24
public EntitySet GetAllEntitySimple(){
25
//代码
.
26
}
27
}
28
}
从上面可以很容易看到就是SQL的四大守护神:SELECT,UPDATE,DELETE,INSERT;一般创建和更新就差一个Id,都是用实体类封装实例,所以我们把他们写作同一个函数里,用枚举(DataAction action)来区分创建还是更新.今天我们要讨论的就是EntitySet类,我们来看一下它的代码:![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
using System;
2
using System.Collections;
3![](/Images/OutliningIndicators/None.gif)
4
namespace NexPlus.Entities
5
{
6
public class EntitySet
7
{
8
ArrayList entities = new ArrayList();
9
int totalRecords = 0;
10
/// <summary>
11
/// 实体总纪录数。
12
/// </summary>
13
public int TotalRecords{
14
get{return totalRecords;}
15
set{totalRecords = value;}
16
}
17
/// <summary>
18
/// 实体项列表。
19
/// </summary>
20
public ArrayList Entities{
21
get{return entities;}
22
}
23
/// <summary>
24
/// 实体中是否有项。
25
/// </summary>
26
public bool HasResults{
27
get{
28
if (entities.Count > 0)
29
return true;
30
return false;
31
}
32
}
33
}
34
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
EntitySet其实就是一个ArrayList的变形版,有了这个类在读取是就可以将对象entitySimple给Add进去了,当我们要用的时候就可以直接调用了:EntitySet set=SimpleManager.GetAllEntitySimple().Entities;如果直接使用SimpleManager.GetAllEntitySimple().Entities前显式转化:(EntitySimple)SimpleManager.GetAllEntitySimple().Entities.
代码很简单,但能构充当十分重要的角色,对于一个网站来说需要很多个实体类集合,这里我们就可以用这个类来替代集合类.能构减少很多代码,当然你可以用List<EntitySimple>,但是有时需要TotalRecords,就不能直接从中读取,像上一篇文章中就不需要加out int totalRecords了,可以直接存在这个类里,我觉得这个类会比较好用,所以就拿出来和大家分享一下,或许你有更好的,希望批评指正!