今天试着研究了一下Repeater的用法
下面是前台定义控件的方法,<span>是定义表头,<td>里面是数据来源的字段名。
<asp:Repeater ID="rpCarList" runat="server" OnItemDataBound="rpCarList_ItemDataBound" >
<HeaderTemplate>
<p style="background-color:#988c6e;width:1000px;padding-top:5px;padding-bottom:5px;margin-left:30px;margin-top:30px;border-radius:5px;color:#fff;font-weight:bold;">
<span style="padding-left:30px;text-align:left;">序号</span>
<span style="padding-left:60px;text-align:left;">申请人</span>
<span style="padding-left:120px;text-align:left;">用车类型</span>
<span style="padding-left:100px;text-align:left;">车牌号</span>
</p>
<table style="margin-left:30px;margin-top:30px;width:1000px;">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:30px;text-align:left;" ><%# Container.ItemIndex+1+initIndex()%></td>
<td style="width:60px;text-align:left; padding-left:20px;"><%#Eval("nigaoren") %></td>
<td style="width:120px;text-align:left; "><%#Eval("head") %></td>
<td style="width:100px;text-align:left; "><%#Eval("cxry") %></td>
</tr>
<tr>
<td colspan="3" style="border-bottom:1px inset #C0D9D9;padding-top:7px;"></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
分页:
<div style="margin-left: 50px;">
<div style="margin: 0 auto; margin-top: 50px; border: 1px solid #fff; font-size: 16px;">
<a>
<div style="border: 1px solid #000; width: 60px; float: left; margin: 5px; text-align: center;"><a style="color: #000">共<asp:Label runat="server" ID="zong"> </asp:Label>页</a></div>
</a>
<a>
<div style="border: 1px solid #000; width: 60px; float: left; margin: 5px; text-align: center;"><a style="color: #000">第<asp:Label runat="server" ID="dangqian"> </asp:Label>页</a></div>
</a>
<a>
<div style="border: 1px solid #000; width: 40px; float: left; margin: 5px; text-align: center;"><a style="color: #000">
<asp:HyperLink ID="first" runat="server" Style="color: #000">首页</asp:HyperLink></a></div>
</a>
<a>
<div style="border: 1px solid #000; width: 60px; float: left; margin: 5px; text-align: center;"><a style="color: #000">
<asp:HyperLink ID="lnkPrev" runat="server" Style="color: #000">上一页</asp:HyperLink></a></div>
</a>
<a>
<div style="border: 1px solid #000; width: 60px; float: left; margin: 5px; text-align: center;"><a style="color: #000">
<asp:HyperLink ID="lnkNext" runat="server" Style="color: #000">下一页</asp:HyperLink></a></div>
</a>
<a>
<div style="border: 1px solid #000; width: 40px; float: left; margin: 5px; text-align: center;"><a style="color: #000">
<asp:HyperLink ID="end" runat="server" Style="color: #000">尾页</asp:HyperLink></a></div>
</a>
</div>
</div>
下面是后台绑定Repeater的数据来源,同时做了分页
private void getUsers()
{
var ds = BindData();
PagedDataSource pag = new PagedDataSource();
pag.AllowPaging = true;// 设置允许分页
pag.PageSize = 10; // 每页显示为3行
pag.DataSource = ds; // 模板绑定数据源
zong.Text = pag.PageCount.ToString(); // 显示总共页数
int CurrentPage;
// 请求页码为不为null设置当前页,否则为第一页
if (Request.QueryString["Page"] != null)
{
CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurrentPage = 1;
}
if (Request.QueryString["PageSize"] != null)
{
pag.PageSize = Convert.ToInt32(Request.QueryString["PageSize"]);
}
else
{
pag.PageSize = 10;
}
pag.CurrentPageIndex = CurrentPage - 1; // 当前页所引为页码-1
dangqian.Text = CurrentPage.ToString(); // 当前页
if (!pag.IsFirstPage)
{
// Request.CurrentExecutionFilePath为当前请求虚拟路径
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);
}
// 如果不是最后一页,通过参数Page设置下一页为当前页+1,否则不显示连接
if (!pag.IsLastPage)
{
// Request.CurrentExecutionFilePath为当前请求虚拟路径
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);
}
//首页
first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
//尾页
end.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + pag.PageCount.ToString();
if (Convert.ToInt32(HttpContext.Current.Request["page"]) > pag.PageCount)
{
first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
}
this.rpCarList.DataSource = pag;
this.rpCarList.DataBind();
}
其中ds是接口取的数据表,DataSet类型。我查了一下,可以用DataSet绑定也可以用List,但List我没试过,需要的话可以试一下。
rpCarList是Repeater的ID。
下面要说到重点了!关于分页后,页面的索引,搜索到最多的就是绑定<%# Container.ItemIndex+1%>,但这个方法绑定出来的每一页都是从1开始,我想要的是根据所有数据的条数排列,承接上一页的条数,即每一页的序号都不同。在GridView中,我们用到的是<%# (Container.DataItemIndex+1).ToString()%>,但在Repeater中Container是没有DataItemIndex这个方法的。所以我只能结合后台方法来绑定索引。代码如下:
protected int initIndex()
{
int CurrentPage;
// 请求页码为不为null设置当前页,否则为第一页
if (Request.QueryString["Page"] != null)
{
CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurrentPage = 1;
}
var pageIndex = (CurrentPage - 1) * 10;
return pageIndex;
}

所以在前台绑定数据源时要写<%# Container.ItemIndex+1+initIndex()%>
这样你得到的序号就每一页都不同啦


asp.net Repeater控件 分页索引 按数据索引 每页不同
原文:https://www.cnblogs.com/zhangbupangpang/p/14986444.html