DataList实现GridView的分页

技术科 2008-12-13 09:48:43
使用DataList如何实现像GridView那种下面是一排数字的分页?
...全文
178 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mahayu1982 2008-12-14
  • 打赏
  • 举报
回复
private int pageSize = 6;

private int i = 1;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["PageCount"] = 1;
ViewState["pageIndex"] = 1;

BinerToDataList();
IList<Topic> topics = GetAllProducts("select * from bbstopic");
ViewState["PageCount"] = topics.Count / pageSize + 1;

i = topics.Count / pageSize+1;//new

StringBuilder myt = new StringBuilder();


while (i > 0)
{
myt.Append("<a href=SectionManager.aspx?p=" + i + ">" + i + "</a> ");
i--;
}

mylink.Text = myt.ToString();
}

if (Request.QueryString["p"] != null)
{
PageIndex = Int32.Parse(Request.QueryString["p"]);
BinerToDataList();
}
}



public int PageIndex
{
get { return (int)ViewState["pageIndex"]; }
set
{
if ((int)value >= 1 && (int)value <= (int)ViewState["PageCount"])
ViewState["pageIndex"] = value;
}
}

public void BinerToDataList()
{
IList<Topic> topics = GetAllProducts("SELECT top " + pageSize + " * FROM [bbstopic] where tid not in(select top " + (PageIndex - 1) * pageSize + " tid from [bbstopic])");
//
this.DataList1.DataSource = topics;
this.DataList1.DataBind();
}

public IList<Topic> GetAllProducts(string strsql)
{
IList<Topic> topics = new List<Topic>();
using (SqlDataReader sdr = SqlHelper.GetReader(strsql))
{
while (sdr.Read())
{
Topic top = new Topic();

top.TID = (int)sdr["TID"];
top.TlastReply = (DateTime)sdr["TlastReply"];
top.TreplyCount = (int)sdr["TreplyCount"];
top.Tstate = (int)sdr["tstate"];
top.Ttime = (DateTime)sdr["Ttime"];

topics.Add(top);
}
}
return topics;
}
}

记得在前台代码中增加一个HyperLink控件
zorro911 2008-12-13
  • 打赏
  • 举报
回复
前几天看到的
http://topic.csdn.net/u/20081130/11/f52eebe9-e6f9-46de-b63a-f637b83aa944.html
gxlqssjf 2008-12-13
  • 打赏
  • 举报
回复
同意,可以自己写函数实现
amingo 2008-12-13
  • 打赏
  • 举报
回复
没有 自带的分页。需要自己实现。
wangying110166 2008-12-13
  • 打赏
  • 举报
回复
datalist 没有自带分页,你可以写代码实现,但不如借用第三方快捷。aspnetpager不错的。
mengxj85 2008-12-13
  • 打赏
  • 举报
回复
直接用GridView不是更好吗?

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧