在Repeater控件内实现分页功能:
页面中代码
<asp:Repeater ID="RptJobList" runat="server" OnItemDataBound="RptJobList_ItemDataBound">
<FooterTemplate>
<td align = "right" height="30" width = "828" colspan="2" style="font-size:10pt;color:#636363;">
<asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hlfir" runat="server" Text="第一页"></asp:HyperLink>
<asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hln" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hlla" runat="server" Text="最后一页"></asp:HyperLink>
转到第
<asp:DropDownList ID="ddlp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlp_SelectedIndexChanged" >
</asp:DropDownList>页
</td>
</FooterTemplate>
</asp:Repeater>
.CS代码:
数据绑定函数[自己补充完整即可]
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;//允许分页
pds.PageSize = 20;//单页显示项数
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
/// <summary>
/// Repeater控件分页 处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void RptJobList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");
HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");
PagedDataSource ps = pds();
string a1 = Request.QueryString["page"];
ps.CurrentPageIndex = Convert.ToInt32(a1);
int n = Convert.ToInt32(ps.PageCount);//n为分页数
int i = Convert.ToInt32(ps.CurrentPageIndex);//i为当前页
Label lblpc = (Label)e.Item.FindControl("lblpc");
//if (!IsPostBack)
//{
for (int j = 0; j < n; j++)
{
ddlp.Items.Add(Convert.ToString(j + 1));
}
//}
string addUrl = "&&testType=" + ddlWorkType.SelectedValue + "&&provinceId=" + ddlProvince.SelectedValue + "&&jobclassId" + ddlJobclass.SelectedValue;
if (i <= 0)
{
lpfirst.Enabled = false;
lpprev.Enabled = false;
lplast.Enabled = true;
lpnext.Enabled = true;
}
else
{
lpprev.NavigateUrl = "?page=" + (i - 1) + addUrl;
}
if (i >= n - 1)
{
lpfirst.Enabled = true;
lplast.Enabled = false;
lpnext.Enabled = false;
lpprev.Enabled = true;
}
else
{
ViewState["i"] = i;
lpnext.NavigateUrl = "?page=" + (i + 1) + addUrl;
}
lpfirst.NavigateUrl = "?page=0" + addUrl; //向本页传递参数page
lplast.NavigateUrl = "?page=" + (n - 1) + addUrl;
ddlp.SelectedIndex = Convert.ToInt32(ps.CurrentPageIndex);//更新下拉列表框中的当前选中页序号
}
}
/// <summary>
/// 转到第N页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)
{
//脚模板中的下拉列表框更改时激发
string pg = Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue) - 1));//获取列表框当前选中项
ViewState["pageCount"] = pg;//
Response.Redirect("parttime_joblist.aspx?page=" + pg + "&&testType=" + ddlWorkType.SelectedValue + "&&provinceId=" + ddlProvince.SelectedValue + "&&jobclassId" + ddlJobclass.SelectedValue); //页面转向
}