前台:


<center>
总页数:<asp:Label ID="Label1" runat="server"></asp:Label>
当前第:<asp:Label ID="Label2" runat="server"></asp:Label>
页<span lang="zh-cn"> 跳转到:第</span><asp:DropDownList
ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<span lang="zh-cn">页</span><br />
<center>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="第一页" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="上一页" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" />
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="最后一页" />
</center>
</center>
总页数:<asp:Label ID="Label1" runat="server"></asp:Label>
当前第:<asp:Label ID="Label2" runat="server"></asp:Label>
页<span lang="zh-cn"> 跳转到:第</span><asp:DropDownList
ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<span lang="zh-cn">页</span><br />
<center>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="第一页" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="上一页" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" />
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="最后一页" />
</center>
</center>
每次使用分页的时候 只用复制最下面两行 即可(可以不用修改控件名)!
后台:


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
bind();
}
int pageCount;//总页数
int currentPage = 1;//第定义当前页
int count = 10;//每页条数
private void bind()
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = GoodsManager.GetAllGoods().DefaultView;
//允许分页
pds.AllowPaging = true;
//设置每页显示记录数
pds.PageSize = count;
//获取总页数
pageCount = pds.PageCount;
this.Label1.Text = pageCount.ToString();
pds.CurrentPageIndex = currentPage - 1;
//当前页
this.Label2.Text = Convert.ToString(currentPage);
//数据绑定 下拉列表
this.RepeaterProductList.DataSource = pds;
this.RepeaterProductList.DataBind();
DropDownList1.Items.Clear();
//绑定dropdownlist
for (int i = 0; i < pageCount; i++)
{
DropDownList1.Items.Add((i+1).ToString());
}
}
//截取字符
public string SubstringStr(string str)
{
if (str.Length > 10)
{
return str.Substring(0, 10);
}
else
{
return str;
}
}
protected void RepeaterProductList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//((LinkButton)e.Item.FindControl("LinkBtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
if (e.CommandName == "Delete")
{
string idstr = e.CommandArgument.ToString();
int id = int.Parse(idstr);
int rs = GoodsManager.DeleteGood(id);
if (rs > 0)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功!')</script>");
bind();
}
else
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!')</script>");
}
}
/// <summary>
/// 第一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{//如果当前不是第一页的时候
if (this.Label2.Text == "1")
{
}
else
{
currentPage = 1;
bind();
}
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{//如果当前不是最后页的时候
if (this.Label1.Text == this.Label2.Text)
{
}
else
{
currentPage = int.Parse(this.Label2.Text) + 1;
this.Label2.Text = currentPage.ToString();
bind();
}
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{//如果当前不是第一页的时候
if (this.Label2.Text != "1")
{
currentPage = int.Parse(this.Label2.Text) - 1;
this.Label2.Text = currentPage.ToString();
bind();
}
}
/// <summary>
/// 最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button4_Click(object sender, EventArgs e)
{//如果当前不是最后一页的时候
if (this.Label1.Text != this.Label2.Text)
{
this.Label2.Text = this.Label1.Text;
currentPage = int.Parse(this.Label2.Text);
bind();
}
}
//页面跳转
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
currentPage = int.Parse(DropDownList1.SelectedItem.Text);
bind();
// DropDownList1.Items.Clear();
}
{
if(!IsPostBack)
bind();
}
int pageCount;//总页数
int currentPage = 1;//第定义当前页
int count = 10;//每页条数
private void bind()
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = GoodsManager.GetAllGoods().DefaultView;
//允许分页
pds.AllowPaging = true;
//设置每页显示记录数
pds.PageSize = count;
//获取总页数
pageCount = pds.PageCount;
this.Label1.Text = pageCount.ToString();
pds.CurrentPageIndex = currentPage - 1;
//当前页
this.Label2.Text = Convert.ToString(currentPage);
//数据绑定 下拉列表
this.RepeaterProductList.DataSource = pds;
this.RepeaterProductList.DataBind();
DropDownList1.Items.Clear();
//绑定dropdownlist
for (int i = 0; i < pageCount; i++)
{
DropDownList1.Items.Add((i+1).ToString());
}
}
//截取字符
public string SubstringStr(string str)
{
if (str.Length > 10)
{
return str.Substring(0, 10);
}
else
{
return str;
}
}
protected void RepeaterProductList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//((LinkButton)e.Item.FindControl("LinkBtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
if (e.CommandName == "Delete")
{
string idstr = e.CommandArgument.ToString();
int id = int.Parse(idstr);
int rs = GoodsManager.DeleteGood(id);
if (rs > 0)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功!')</script>");
bind();
}
else
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!')</script>");
}
}
/// <summary>
/// 第一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{//如果当前不是第一页的时候
if (this.Label2.Text == "1")
{
}
else
{
currentPage = 1;
bind();
}
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{//如果当前不是最后页的时候
if (this.Label1.Text == this.Label2.Text)
{
}
else
{
currentPage = int.Parse(this.Label2.Text) + 1;
this.Label2.Text = currentPage.ToString();
bind();
}
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{//如果当前不是第一页的时候
if (this.Label2.Text != "1")
{
currentPage = int.Parse(this.Label2.Text) - 1;
this.Label2.Text = currentPage.ToString();
bind();
}
}
/// <summary>
/// 最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button4_Click(object sender, EventArgs e)
{//如果当前不是最后一页的时候
if (this.Label1.Text != this.Label2.Text)
{
this.Label2.Text = this.Label1.Text;
currentPage = int.Parse(this.Label2.Text);
bind();
}
}
//页面跳转
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
currentPage = int.Parse(DropDownList1.SelectedItem.Text);
bind();
// DropDownList1.Items.Clear();
}
灰色部分是需要更改的,其他部分可以直接拿来复用!