准备工作:
1.官网下载:AspNetPager.dll(百度下载,免费提供但是免除不了版权限制,正式商业可购买)
2.数据库操作类DataBase(自己写的,读者可以自己随意)
开始:
aspx代码区域:
<asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <div class="successshow"> <ul> <li><a href="<%#Eval("url")%>"> <img src="upload/images/<%#Eval("imgurl")%>" alt="<%#Eval("title")%>" style="float: left; width: 150px; height: 100px;" /></a><p> <asp:Label ID="lblContent" runat="server"><%#Eval("content")%></asp:Label> </p> </li> <li><span>建站时间:</span> <asp:Label ID="lblTime" runat="server"><%#Eval("time")%></asp:Label> <span>地址:</span><span><asp:HyperLink ID="hlUrl" runat="server"><%#Eval("url")%></asp:HyperLink></span> <span>风格:</span><asp:Label ID="lblStyle" runat="server"><%#Eval("stype")%></asp:Label> <span>浏览次数:</span><asp:Label ID="lblViews" runat="server"><%#Eval("views")%></asp:Label> </li> </ul> </div> </ItemTemplate> </asp:Repeater> <div class="DataPager"> <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" OnPageChanged="AspNetPager1_PageChanged" PageSize="6" ShowNavigationToolTip="True">//本页显示的条数 </webdiyer:AspNetPager> </div>
aspx.cs后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection conn = DataBase.GetConn();//创建数据库连接 SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select count(*) from T_Success"; AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();//注意此处不要遗漏 conn.Close(); BindData(); } } public void BindData() { SqlConnection conn = DataBase.GetConn(); string sql = "select * from T_Success order by id desc";//这句在大型数据中应该用:select top查询语句 SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "temptbl");//此处重要,涉及到AspNetPager1得所有方法,添加AspNetPager.dll引用后,会自动在aspx页面头部添加<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>声明,如果出错,请检查! DataTable dt = ds.Tables["temptbl"]; //绑定Repeater1数据源 Repeater1.DataSource = dt; Repeater1.DataBind(); } //添加AspNetPager1的AspNetPager1_PageChanged事件 protected void AspNetPager1_PageChanged(object sender, EventArgs e) { // AspNetPager1.CurrentPageIndex = e.NewPageIndex; BindData(); }
接下来分页的样式,等属性就简单了,本实例只适合Repeater的一种分页方式,当然读者不要拘于他人的思想限制,这始终是别人的东西,因为我们学习的是思想,而不是死死的代码!
亲爱的各位战友:www.xiaomi365.com 小米在线 进取随便浏览,支持下我这个苦逼的程序猿。不容易啊!感谢你们了!!