关于GridView分页

来源:互联网 发布:违规网络音乐产品名单 编辑:程序博客网 时间:2024/06/10 00:22

在GridView中实现分页的效果方法很简单,只需要在“GridView任务”对话框中进行设置就可以了。在“GridView任务”对话框中,选择“启用分页”命令,这样建立起简单的分页效果。

在使用“启用分页”命令的时候要注意两点。

(1) 是否允许分页

GridView的AllowPaging属性。AllowPaging:是否允许分页。如果AllowPaging=“true”就是允许分页。否则就是不允许使用分页。

(2)每页记录数

GridView的PageSize属性。在GridView控件的属性中可以设置每页显示的数据记录的个数。默认情况下PageSize的值是10,也可以根据需要进行设置。

如果想要对分页编码进行设置的话,可以在HTML代码中为GridView控件添加分页导航条形式代码。也就是启用GridView的PagerSettings属性,在PagerSettings属性中可以设置根据需要设置Mode的值,来实现分页编码的显示效果。

<PagerSettings

Mode = "NextPreviousFirstLast"

FirstPageText = "第一页"

LastPageText = "末页">

</PagerSettings>

注意:

PagerSettings属性的Mode:Numeric,NextPrevious,NextPreviousFirstLast,NumericFirstLast。有这四种,可以根据不同需要进行不同的选择设置。

自动设置分页效果
现在想要在GridView控件上显示如下页码信息:总页数、当前页、首页、上一页、下一页、尾页。

创建总页数

<asp:Label ID="Lab_PageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>

创建但前页

<asp:Label ID="Lab_CurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>

创建首页

<asp:LinkButton ID="LBtn_FirstPage" runat="server" CommandArgument="First" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>

创建上一页

<asp:LinkButton ID="LBtn_PreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>

创建下一页

<asp:LinkButton ID="LBtn_NextPage" runat="server" CommandArgument="Next" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>

创建尾页

<asp:LinkButton ID="LBtn_LastPage" runat="server" CommandArgument="Last" CommandName="Page"

Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>

对应后台代码

public void GetDataSet()
{
string zhuangtmc;
zhuangtmc = Convert.ToString(DropDownList1.Text);
string sql;
sql = "Select L.LunWBH,L.LunWBT,Z.ZhuangTMC,L.ZhuCYHM,B.BianJM,L.TouGRQ From T_LunWXX L ,T_LunWZhT Z,T_BianJPL B ";
sql += "Where L.ZhuangTBH=Z.ZhuangTBH AND B.LunWBH=L.LunWBH AND";
sql += " B.BianJM='" + zhucyhm + "'";
if (zhuangtmc != "")
{
sql += " And Z.ZhuangTMC='" + zhuangtmc + "'";
}
sql += " order By TouGRQ";
CommonDB = new Common();
GridView1.DataSource = CommonDB.DataSource(sql);
GridView1.DataBind();

//用lblCurrentIndex来显示当前页的页数。
LabelCurrentPage.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
//用LblPageCount来显示当前数据的总页数。
LabelPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";
//用LblrecordCount来显示数据的总条数。
LabelRecordCount.Text = "总共 " + CommonDB.DataSets(sql).Tables[0].Rows.Count.ToString() + " 条";

// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
//点击首页设定的值为1。
LinkButtonFirstPage.CommandName = "1";
//点击‘上一页’
LinkButtonPreviousPage.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
//点击‘下一页’
LinkButtonNextPage.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
//点击‘尾页’
LinkButtonLastPage.CommandName = GridView1.PageCount.ToString();

原创粉丝点击