在做善良公社项目,遇到一个关于分页的问题。我是用Repeater绑定的数据,Repeater控件轻量级,完全的自定义,正是因为这些优点,所以说没有自动分页的功能。这样,该如何实现分页呢?
再查找相关资料后,借助PageDataSource,实现了分页功能。
前台页面:
<div style="margin-left: 120px; margin-right: 120px;">
<table id="theTable">
<asp:Repeater ID="userinfo" runat="server" >
<HeaderTemplate>
<table id="DefultUserInfo" cellspacing="1">
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td class="auto-style3" rowspan="5">
<%--<img src="img/网站默认头像.jpg" style="height: 100px; width: 80px" /></a></td>--%>
<img src="<%#((System.Data.DataRowView )Container.DataItem)["HeadImg"] %>" style="height: 100px; width: 100px" /></a></td>
<td class="auto-style4">姓名:<a href='NeedHelpSpace.aspx?UserID=<%#Eval("UserID")%>'> <%# ((System.Data.DataRowView )Container.DataItem)["RealName"] %></a></td>
<td class="auto-style6">性别:<%# ((System.Data.DataRowView )Container.DataItem)["Sex"] %></td>
</tr>
<tr>
<td class="auto-style2">职业:学生</td>
<td class="auto-style7" colspan="2">善良积分:30</td>
</tr>
<tr>
<td class="auto-style5">生日:<%# ((System.Data.DataRowView )Container.DataItem)["Birthday"] %></td>
</tr>
<tr>
<td class="auto-style5" colspan="3">个人说明:<%# ((System.Data.DataRowView )Container.DataItem)["IsHelp"] %></td>
</tr>
<tr>
<td><a href='MailConnect.aspx?UserName=<%#Eval("UserName")%>' οnclick="return CheckSession();">联系TA</a> <a href="#" οnclick="CheckSession()">加为好友</a></td>
</tr>
<tr>
<td class="auto-style5" colspan="3">
<hr style="border-style: dashed; color: #DDDDDD;" />
</td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</table>
<asp:HyperLink ID="lnkUp" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkDown" runat="server" >下一页</asp:HyperLink>
<asp:Label ID="lbl_info" runat="server" Text="当前第x页,共x页"></asp:Label><br />
</div>
后台实现:在PageLoad中实现
PagedDataSource pds = new PagedDataSource();
DataSet ds = new DataSet();
ds = MemberSearch();//通过方法获取数据源
pds.DataSource = ds.Tables[0].DefaultView;
//允许分页
pds.AllowPaging = true;
//设置页面显示数据的条数
pds.PageSize = 5;
int currentPage = Convert.ToInt32(Request["page"]);//从网址中获取参数page
//设当前页
pds.CurrentPageIndex = currentPage;
//设几个超链接
if (!pds.IsFirstPage)
{
//如果不是第一页
lnkUp.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1) ;
}
//如果不是最后一页
if (!pds.IsLastPage)
{
lnkDown.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1) ;
}
lbl_info.Text = "第" + (currentPage + 1) + "页、共" + pds.PageCount + "页";
userinfo.DataSource = pds; //userinfo为repeater控件的ID号
userinfo.DataBind();
这样就实现了Repeater的真分页。
显示结果:
总结:
Hyperlink是什么?Hyperlink是WWW上使用较多的一种技巧,它通过事先定义好的关键字或者图形,只要你用鼠标点击该段文字或者图形,就可以自动连接上相对应的其它文件,通过这种方式,就可以实现你想要的效果。
这种效果也能用LinkButton实现,因为我的页面时HTML页面,所以LinkButton事先失败。