数据显示控件的通用分页代码
以下是我自己做系统,积累的,我把这些有用的代码提取出来,供大家来使用,希望可以帮助大家!
<table align="left" cellpadding="0" cellspacing="0" style="width: 500px">
<tr>
<td style="font-size: 9pt; width: 500px; height: 15px; text-align: left">
<asp:Label ID="labCP" runat="server" Text="当前页码为:"></asp:Label>
[
<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>
]
<asp:Label ID="labTP" runat="server" Text="总页码为:"></asp:Label>
[
<asp:Label ID="labBackPage" runat="server"></asp:Label>
]<asp:LinkButton ID="lnkbtnOne" runat="server" Font-Underline="False" ForeColor="Red"
OnClick="lnkbtnOne_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnUp" runat="server" Font-Underline="False" ForeColor="Red"
OnClick="lnkbtnUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnNext" runat="server" Font-Underline="False" ForeColor="Red"
OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnBack" runat="server" Font-Underline="False" ForeColor="Red"
OnClick="lnkbtnBack_Click" Width="48px">最后一页</asp:LinkButton>
</td>
</tr>
</table>
后台代码:
protected void bind()
{
//取得当前页的页码
int curpage = Convert.ToInt32(this.labPage.Text);
//使用PagedDataSource类实现DataList控件的分页功能
PagedDataSource ps = new PagedDataSource();
//获取数据集
//DataSet ds = CC.GetDataSet("select * from tb_News where style='" + strStyle + "' order by issueDate Desc", "tbNews");
//ps.DataSource = ds.Tables["tbNews"].DefaultView;
//是否可以分页
ps.DataSource = NewsInfomessage.select_by_state(1);
ps.AllowPaging = true;
//显示的数量
ps.PageSize =3;
//取得当前页的页码
ps.CurrentPageIndex = curpage - 1;
this.lnkbtnUp.Enabled = true;
this.lnkbtnNext.Enabled = true;
this.lnkbtnBack.Enabled = true;
this.lnkbtnOne.Enabled = true;
if (curpage == 1)
{
//不显示第一页按钮
this.lnkbtnOne.Enabled = false;
//不显示上一页按钮
this.lnkbtnUp.Enabled = false;
}
if (curpage == ps.PageCount)
{
//不显示下一页
this.lnkbtnNext.Enabled = false;
//不显示最后一页
this.lnkbtnBack.Enabled = false;
}
//显示分页数量
this.labBackPage.Text = Convert.ToString(ps.PageCount);
//绑定DataList控件
data.DataSource = ps;
//data.DataKeyField=
data.DataBind();
//this.dlNews.DataSource = ps;
//this.dlNews.DataKeyField = "id";
//this.dlNews.DataBind();
}
//第一页
protected void lnkbtnOne_Click(object sender, EventArgs e)
{
this.labPage.Text = "1";
this.bind();
}
//上一页
protected void lnkbtnUp_Click(object sender, EventArgs e)
{
this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) - 1);
this.bind();
}
//下一页
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) + 1);
this.bind();
}
//最后一页
protected void lnkbtnBack_Click(object sender, EventArgs e)
{
this.labPage.Text = this.labBackPage.Text;
this.bind();
}