前台代码:
<div class="tggl_2">
每页显示
<asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged">
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>
<asp:ListItem Value="40">40</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList>
条 共 <asp:Label ID="lblRecordCount" runat="server" Text=""></asp:Label> 条信息
第 <asp:Label ID="lblPageIndex" runat="server" Text=""></asp:Label> 页 /
共 <asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label> 页
<asp:Button ID="btnFirst" runat="server" Text="首 页" OnClick="btnFirst_Click" />
<asp:Button ID="btnPrev" runat="server" Text="上一页" OnClick="btnPrev_Click" />
<asp:Button ID="btnNext" runat="server" Text="下一页" OnClick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Text="尾 页" OnClick="btnLast_Click" />
转到第<input type="text" id="textfield" size="3" onkeyup="javascript:checkIsInt();"
runat="server" />
页
<asp:Button ID="btnGoPageIndex" class="smallbutton" runat="server" Text="GO" Style="border: 0;
width: 20px; height: 15px;" OnClick="btnGoPageIndex_Click" />
<input name="chkall" type="checkbox" id="chkall" value="select" onclick="CheckAll(this.form)" /><label
for="chkAll">全选</label>
<asp:Button ID="btnDel" runat="server" OnClick="btnDel_Click" Text="批量删除" OnClientClick="return delIt()" />
</div>
代码生成如图所示:
后台代码:
#region 数据绑定及分页
//分页控制显示方法
public void contrlrepeater()
{
DataSet ds = (DataSet)ViewState["ds"];//绑定的数据集
if (ds.Tables[0].Rows.Count > 0)
{
//分页设置并绑定数据
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;//运行分页
pds.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue);//设置每页显示的条数
pds.CurrentPageIndex = Convert.ToInt32(this.lblPageIndex.Text) - 1;//设置当前页
Repeater1.DataSource = pds;
this.lblPageCount.Text = pds.PageCount.ToString();
this.lblPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();
this.lblRecordCount.Text = ds.Tables[0].Rows.Count.ToString();
this.btnFirst.Enabled = true;
this.btnPrev.Enabled = true;
this.btnNext.Enabled = true;
this.btnLast.Enabled = true;
if (pds.CurrentPageIndex < 1)
{
this.btnFirst.Enabled = false;
this.btnPrev.Enabled = false;
}
if (pds.CurrentPageIndex == pds.PageCount - 1)
{
this.btnNext.Enabled = false;
this.btnLast.Enabled = false;
}
if (pds.PageCount == 1)
{
this.btnGoPageIndex.Enabled = false;
}
Repeater1.DataBind();
}
else
{
this.lblPageCount.Text = "0";
this.lblPageIndex.Text = "0";
this.lblRecordCount.Text = "0";
this.ddlPageSize.Enabled = false;
this.btnFirst.Enabled = false;
this.btnPrev.Enabled = false;
this.btnNext.Enabled = false;
this.btnLast.Enabled = false;
this.btnGoPageIndex.Enabled = false;
this.btnDel.Enabled = false;
Repeater1.DataSource = null;
Repeater1.DataBind();
}
}
//更改每页显示条数
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblPageIndex.Text = "1";
this.contrlrepeater();
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnFirst_Click(object sender, EventArgs e)
{
this.lblPageIndex.Text = "1";
this.contrlrepeater();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnPrev_Click(object sender, EventArgs e)
{
this.lblPageIndex.Text = Convert.ToString(Convert.ToInt32(this.lblPageIndex.Text) - 1);
this.contrlrepeater();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnNext_Click(object sender, EventArgs e)
{
this.lblPageIndex.Text = Convert.ToString(Convert.ToInt32(this.lblPageIndex.Text) + 1);
this.contrlrepeater();
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnLast_Click(object sender, EventArgs e)
{
this.lblPageIndex.Text = this.lblPageCount.Text;
this.contrlrepeater();
}
/// <summary>
/// 跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnGoPageIndex_Click(object sender, EventArgs e)
{
if (this.textfield.Value.Trim().ToString() != "")
{
this.lblPageIndex.Text = this.textfield.Value;
this.contrlrepeater();
}
}
#endregion