求ASP.NET中DataList分页实现代码
2个回答
展开全部
下面用到的class自己写哈,你爱怎么布局就怎么布局
aspx中的代码:
<div class="fanye">
<asp:Label ID="Label1" runat="server" CssClass="lbl" Text="第"></asp:Label>
<asp:Label ID="lblcurrent" runat="server" CssClass="lbl" Text="1"></asp:Label>
<asp:Label ID="Label3" runat="server" CssClass="lbl" Text="页 共"></asp:Label>
<asp:Label ID="lblall" runat="server" CssClass="lbl" Text="10"></asp:Label>
<asp:Label ID="Label5" runat="server" CssClass="lbl" Text="页 "></asp:Label>
<asp:LinkButton ID="lblfirst" runat="server" CssClass="lbl"
onclick="lblfirst_Click" >首页</asp:LinkButton>
<asp:LinkButton ID="lbtnform" runat="server" CssClass="lbl" onclick="lbtnform_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnnext" runat="server" CssClass="lbl" onclick="lbtnnext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbllast" runat="server" CssClass="lbl"
onclick="lbllast_Click" >尾页</asp:LinkButton>
<asp:TextBox ID="txtnum" runat="server" CssClass="txt"></asp:TextBox>
<asp:Button ID="btngo" runat="server" CssClass="btn" Text="前往"
onclick="btngo_Click" />
</div>
cs里面的代码:
private void bind()//绑定数据源
{
DataTable dt = new DataTable();
dt = 数据源;
datalist.DataSource = dt;
datalist.DataBind();
//创建数据源
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dt.DefaultView;
//允许分页
pds.AllowPaging = true;
//设置每页显示记录数
pds.PageSize = int.Parse("18");
//获取总页数
pageCount = pds.PageCount;
this.lblall.Text = pageCount.ToString();
pds.CurrentPageIndex = currentPage - 1;
//当前页
this.lblcurrent.Text = Convert.ToString(currentPage);
//数据绑定
this.datalist.DataSource = pds;
this.datalist.DataBind();
}
int pageCount;//总页数
int currentPage = 1;//第定义当前页
//首页
protected void lblfirst_Click(object sender, EventArgs e)
{
if (this.lblcurrent.Text == "1")
{ }
else
{
currentPage = 1;
bind();
}
}
//上一页
protected void lbtnform_Click(object sender, EventArgs e)
{
if (this.lblcurrent.Text != "1")
{
currentPage = int.Parse(this.lblcurrent.Text) - 1;
this.lblcurrent.Text = currentPage.ToString();
bind();
}
}
//下一页
protected void lbtnnext_Click(object sender, EventArgs e)
{
if (this.lblall.Text == this.lblcurrent.Text)
{
}
else
{
currentPage = int.Parse(this.lblcurrent.Text) + 1;
this.lblcurrent.Text = currentPage.ToString();
bind();
}
}
//尾页
protected void lbllast_Click(object sender, EventArgs e)
{
if (this.lblall.Text != this.lblcurrent.Text)
{
this.lblcurrent.Text = this.lblall.Text;
currentPage = int.Parse(this.lblall.Text);
bind();
}
}
//页面跳转
protected void btngo_Click(object sender, EventArgs e)
{
if (int.Parse(txtnum.Text) > int.Parse(lblall.Text) || int.Parse(txtnum.Text) < 0)
{ }
else
{
currentPage = Convert.ToInt32(this.txtnum.Text);
bind();
}
}
aspx中的代码:
<div class="fanye">
<asp:Label ID="Label1" runat="server" CssClass="lbl" Text="第"></asp:Label>
<asp:Label ID="lblcurrent" runat="server" CssClass="lbl" Text="1"></asp:Label>
<asp:Label ID="Label3" runat="server" CssClass="lbl" Text="页 共"></asp:Label>
<asp:Label ID="lblall" runat="server" CssClass="lbl" Text="10"></asp:Label>
<asp:Label ID="Label5" runat="server" CssClass="lbl" Text="页 "></asp:Label>
<asp:LinkButton ID="lblfirst" runat="server" CssClass="lbl"
onclick="lblfirst_Click" >首页</asp:LinkButton>
<asp:LinkButton ID="lbtnform" runat="server" CssClass="lbl" onclick="lbtnform_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnnext" runat="server" CssClass="lbl" onclick="lbtnnext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbllast" runat="server" CssClass="lbl"
onclick="lbllast_Click" >尾页</asp:LinkButton>
<asp:TextBox ID="txtnum" runat="server" CssClass="txt"></asp:TextBox>
<asp:Button ID="btngo" runat="server" CssClass="btn" Text="前往"
onclick="btngo_Click" />
</div>
cs里面的代码:
private void bind()//绑定数据源
{
DataTable dt = new DataTable();
dt = 数据源;
datalist.DataSource = dt;
datalist.DataBind();
//创建数据源
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dt.DefaultView;
//允许分页
pds.AllowPaging = true;
//设置每页显示记录数
pds.PageSize = int.Parse("18");
//获取总页数
pageCount = pds.PageCount;
this.lblall.Text = pageCount.ToString();
pds.CurrentPageIndex = currentPage - 1;
//当前页
this.lblcurrent.Text = Convert.ToString(currentPage);
//数据绑定
this.datalist.DataSource = pds;
this.datalist.DataBind();
}
int pageCount;//总页数
int currentPage = 1;//第定义当前页
//首页
protected void lblfirst_Click(object sender, EventArgs e)
{
if (this.lblcurrent.Text == "1")
{ }
else
{
currentPage = 1;
bind();
}
}
//上一页
protected void lbtnform_Click(object sender, EventArgs e)
{
if (this.lblcurrent.Text != "1")
{
currentPage = int.Parse(this.lblcurrent.Text) - 1;
this.lblcurrent.Text = currentPage.ToString();
bind();
}
}
//下一页
protected void lbtnnext_Click(object sender, EventArgs e)
{
if (this.lblall.Text == this.lblcurrent.Text)
{
}
else
{
currentPage = int.Parse(this.lblcurrent.Text) + 1;
this.lblcurrent.Text = currentPage.ToString();
bind();
}
}
//尾页
protected void lbllast_Click(object sender, EventArgs e)
{
if (this.lblall.Text != this.lblcurrent.Text)
{
this.lblcurrent.Text = this.lblall.Text;
currentPage = int.Parse(this.lblall.Text);
bind();
}
}
//页面跳转
protected void btngo_Click(object sender, EventArgs e)
{
if (int.Parse(txtnum.Text) > int.Parse(lblall.Text) || int.Parse(txtnum.Text) < 0)
{ }
else
{
currentPage = Convert.ToInt32(this.txtnum.Text);
bind();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询