[b]前台:添加一个Repeater 控件 四个LinkButton 控件[/b]
<div class="information">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="信息与计算科学"></asp:Label></HeaderTemplate>
<ItemTemplate>
<div>
<image src=""></image>
<asp:Label ID="name" runat="server"><%#DataBinder.Eval(Container.DataItem,"name") %></asp:Label><asp:Label
ID="time" runat="server"><%#DataBinder.Eval(Container.DataItem,"name") %></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<div>
当前页码:[<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>] 总页码为:[<asp:Label
ID="totalPage" runat="server" Text="totalPage"></asp:Label>]
<asp:LinkButton ID="lnkbnOne" runat="server" OnClick="lnkbnOne_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="lnkbnUp" runat="server" OnClick="lnkbnUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lnkbnNext" runat="server" OnClick="lnkbnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbnBack" runat="server" OnClick="lnkbnBack_Click">末 页</asp:LinkButton>
</div>
</div>
后台:封装了一个数据绑定的方法,调用执行就能够实现绑定
public void dbBind()
{
int curpage = Convert.ToInt32(labPage.Text);
PagedDataSource ps = new PagedDataSource();
BLL.tb_teachers tIntroduce = new tb_teachers();//自己写的一个类
string sql = "proId=1";
DataSet ds = tIntroduce.GetList(sql);//从数据库查询出数据
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true;//允许分页
ps.PageSize = 2;//每一页显示两条记录
ps.CurrentPageIndex = curpage - 1;//获取当前页
lnkbnOne.Enabled = true;
lnkbnUp.Enabled = true;
lnkbnNext.Enabled = true;
lnkbnBack.Enabled = true;
if (curpage==1)
{
lnkbnOne.Enabled = false;//不显示第一页按钮
lnkbnUp.Enabled = false;//不显示上一页按钮
}
if (curpage==ps.PageCount)
{
lnkbnNext.Enabled = false;//不显示下一页
lnkbnBack.Enabled = false;//不显示最后一页
}
totalPage.Text = Convert.ToString(ps.PageCount);
Repeater1.DataSource = ps;
Repeater1.DataBind();
}
//回到首页
protected void lnkbnOne_Click(object sender, EventArgs e)
{
labPage.Text = "1";
dbBind();
}
//上一页
protected void lnkbnUp_Click(object sender, EventArgs e)
{
labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text)-1);
dbBind();
}
//下一页
protected void lnkbnNext_Click(object sender, EventArgs e)
{
labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) + 1);
}
//
protected void lnkbnBack_Click(object sender, EventArgs e)
{
labPage.Text =totalPage.Text ;
dbBind();
}
<div class="information">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="信息与计算科学"></asp:Label></HeaderTemplate>
<ItemTemplate>
<div>
<image src=""></image>
<asp:Label ID="name" runat="server"><%#DataBinder.Eval(Container.DataItem,"name") %></asp:Label><asp:Label
ID="time" runat="server"><%#DataBinder.Eval(Container.DataItem,"name") %></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<div>
当前页码:[<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>] 总页码为:[<asp:Label
ID="totalPage" runat="server" Text="totalPage"></asp:Label>]
<asp:LinkButton ID="lnkbnOne" runat="server" OnClick="lnkbnOne_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="lnkbnUp" runat="server" OnClick="lnkbnUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lnkbnNext" runat="server" OnClick="lnkbnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbnBack" runat="server" OnClick="lnkbnBack_Click">末 页</asp:LinkButton>
</div>
</div>
后台:封装了一个数据绑定的方法,调用执行就能够实现绑定
public void dbBind()
{
int curpage = Convert.ToInt32(labPage.Text);
PagedDataSource ps = new PagedDataSource();
BLL.tb_teachers tIntroduce = new tb_teachers();//自己写的一个类
string sql = "proId=1";
DataSet ds = tIntroduce.GetList(sql);//从数据库查询出数据
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true;//允许分页
ps.PageSize = 2;//每一页显示两条记录
ps.CurrentPageIndex = curpage - 1;//获取当前页
lnkbnOne.Enabled = true;
lnkbnUp.Enabled = true;
lnkbnNext.Enabled = true;
lnkbnBack.Enabled = true;
if (curpage==1)
{
lnkbnOne.Enabled = false;//不显示第一页按钮
lnkbnUp.Enabled = false;//不显示上一页按钮
}
if (curpage==ps.PageCount)
{
lnkbnNext.Enabled = false;//不显示下一页
lnkbnBack.Enabled = false;//不显示最后一页
}
totalPage.Text = Convert.ToString(ps.PageCount);
Repeater1.DataSource = ps;
Repeater1.DataBind();
}
//回到首页
protected void lnkbnOne_Click(object sender, EventArgs e)
{
labPage.Text = "1";
dbBind();
}
//上一页
protected void lnkbnUp_Click(object sender, EventArgs e)
{
labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text)-1);
dbBind();
}
//下一页
protected void lnkbnNext_Click(object sender, EventArgs e)
{
labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) + 1);
}
//
protected void lnkbnBack_Click(object sender, EventArgs e)
{
labPage.Text =totalPage.Text ;
dbBind();
}