给大家分享下我自认为GridView 最简单的分页,
前台代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="标题"> <ItemTemplate><%# Eval("title") %></ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="200px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="产品简介"> <ItemTemplate><%# Eval("conter") %></ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="下载">
<ItemTemplate>
<a href='<%# Eval("address") %>'>下载</a>
</ItemTemplate>
<ControlStyle Font-Bold="True" Font-Overline="True" />
<HeaderStyle HorizontalAlign="Center" Width="200px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<RowStyle Height="35px" />
</asp:GridView>
<div style="float:right">
<asp:HyperLink ID="HyperLink1" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="HyperLink3" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="HyperLink4" runat="server">尾页</asp:HyperLink>
</div>
后台代码:
protected void Bind()
{
//定义记录总数
//int total;
//定义当前页码
int pageindex;
//获取当前页码(从查询字符串中获取)
if (!int.TryParse(Request.QueryString["page"], out pageindex))
{
pageindex = 1;
}
//11为显示的当前页显示11条记录。
//获取指定类别,指定页码的数据,并进行绑定显示
GridView1.DataSource = DownManager.GetsPage(pageindex, 11);
GridView1.DataBind();
//计算总的页数
int totalpage = (int)Math.Ceiling(Convert.ToDouble(DownManager.getsNum()) / 11F);
//NavigateUrl 获取或者设置链接目标
//设置分页
HyperLink1.NavigateUrl = "./GoodsDown.aspx?goodsId=" + Request.QueryString["id"];
HyperLink2.NavigateUrl = "./GoodsDown.aspx?goodsId=" + Request.QueryString["id"] + "&page=" + (pageindex > 1 ? pageindex - 1 : 1);
HyperLink3.NavigateUrl = "./GoodsDown.aspx?goodsId=" + Request.QueryString["id"] + "&page=" + (pageindex < totalpage ? pageindex + 1 : totalpage);
HyperLink4.NavigateUrl = "./GoodsDown.aspx?goodsId=" + Request.QueryString["id"] + "&page=" + totalpage;
}
注:page id 值的获取。