分页:所有的数据绑定控件都可以用的
2011-07-15 11:06
225 查看
自己写了一个分页方法,数据绑定控件可以是Repeater、DataList、。。。等等都行,因为原理是每次重新获取页面所需的数据,然后再重新绑定。
后台代码:
效果图:
PS:
有人问Article类是干什么的,其实这个类只是封装了对我的数据表Article的操作而已,Article类我只写了两个方法,一个是获取分页数据GetPageData
另外一个是获取数据的总数:GetTotalCount()
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData() { Article a = new Article(); pageCount = 10; //设置每页记录条数 totalCount = a.GetTotalCount(); //获取记录总数 rpArticle.DataSource = a.GetPageData(GetCurrentIndex(), pageCount);//获取分页数据 rpArticle.DataBind(); ResetButtonStatus(pageCount,totalCount); } #region 分页处理 //使用例子 //private void BindData() //{ // Article a = new Article(); // pageCount = 10; //设置每页记录条数 // totalCount = a.GetTotalCount(); //获取记录总数 // rpArticle.DataSource = a.GetPageData(GetCurrentIndex(), pageCount);//获取该页所需的数据 // rpArticle.DataBind(); // ResetButtonStatus(pageCount, totalCount); //} //获取分页数据SQL格式,参数@PageIndex:第几页数据,参数@PageCount:每页的记录条数 //sb.Append("select * from "); //sb.Append("(select ROW_NUMBER() over(order by ArticleID desc) as row_number,T_Article.Title "); //sb.Append("from T_Article ) t "); //sb.Append("where row_number > (@PageIndex-1)*@PageCount "); //sb.Append("and row_number <= (@PageIndex-1)*@PageCount + @PageCount"); /// <summary> /// 每页的记录条数 /// </summary> public int pageCount { get; set; } /// <summary> /// 记录总数 /// </summary> public int totalCount { get; set; } /// <summary> /// 获取当前页 /// </summary> /// <returns></returns> private int GetCurrentIndex() { int index = 1; if (ViewState["currentIndex"] == null) { ViewState["currentIndex"] = index; } else { index = Convert.ToInt32(ViewState["currentIndex"]); } return index; } /// <summary> /// 重置按钮 /// </summary> /// <param name="pageCount"></param> private void ResetButtonStatus(int pageCount,int totalCount) { lblTotal.Text = totalCount.ToString(); int maxPage = totalCount % pageCount == 0 ? totalCount / pageCount : totalCount / pageCount + 1; lblTotalPage.Text = maxPage.ToString(); ddlCurrentPage.Items.Clear(); for (int i = 1; i <= maxPage; i++) { ddlCurrentPage.Items.Add(new ListItem(i.ToString(), i.ToString())); } ddlCurrentPage.SelectedValue = GetCurrentIndex().ToString(); if (GetCurrentIndex() == maxPage) { lbtnNext.Enabled = false; //lbtnNext.Text = "已到末页"; lbtnLast.Enabled = false; } else { lbtnNext.Enabled = true; lbtnNext.Text = "下一页"; lbtnLast.Enabled = true; } if (GetCurrentIndex() == 1) { lbtnPrev.Enabled = false; //lbtnPrev.Text = "已到首页"; lbtnFirst.Enabled = false; } else { lbtnPrev.Enabled = true; lbtnPrev.Text = "上一页"; lbtnFirst.Enabled = true; } } //上一页 protected void lbtnPrev_Click(object sender, EventArgs e) { ViewState["currentIndex"] = Convert.ToInt32(ViewState["currentIndex"]) - 1; BindData(); } //下一页 protected void lbtnNext_Click(object sender, EventArgs e) { ViewState["currentIndex"] = Convert.ToInt32(ViewState["currentIndex"]) + 1; BindData(); } //页面跳转 protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e) { ViewState["currentIndex"] = Convert.ToInt32(ddlCurrentPage.SelectedItem.Text); BindData(); } //首页 protected void lbtnFirst_Click(object sender, EventArgs e) { ViewState["currentIndex"] = 1; BindData(); } //尾页 protected void lbtnLast_Click(object sender, EventArgs e) { ViewState["currentIndex"] = Convert.ToInt32(lblTotalPage.Text); BindData(); } #endregion
前台html:
<form id="form1" runat="server"> <div> <asp:Repeater ID="rpArticle" runat="server" EnableViewState="False"> <ItemTemplate> <%#Eval("Title") %><br /> </ItemTemplate> </asp:Repeater> </div> <div id="pageshow" style="font-size: 14px; color: #0C0C0C; font-weight: 500;"> <asp:LinkButton ID="lbtnFirst" runat="server" OnClick="lbtnFirst_Click">首页</asp:LinkButton> <asp:LinkButton ID="lbtnPrev" runat="server" OnClick="lbtnPrev_Click">上一页</asp:LinkButton> <asp:LinkButton ID="lbtnNext" runat="server" OnClick="lbtnNext_Click">下一页</asp:LinkButton> <asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">尾页</asp:LinkButton> <asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCurrentPage_SelectedIndexChanged"> </asp:DropDownList> /<asp:Label ID="lblTotalPage" runat="server"></asp:Label> 总共 <asp:Label ID="lblTotal" runat="server"></asp:Label> 条记录</div> </form>
效果图:
PS:
有人问Article类是干什么的,其实这个类只是封装了对我的数据表Article的操作而已,Article类我只写了两个方法,一个是获取分页数据GetPageData
/// <summary> /// 获取分页数据 /// </summary> /// <param name="PageIndex">页索引</param> /// <param name="PageCount">页大小</param> /// <returns>DataSet</returns> public DataSet GetPageData(int PageIndex, int PageCount) { using (SqlConnection con = new SqlConnection()) { con.ConnectionString = ConfigurationManager.ConnectionStrings["CollegeWebConnectionString"].ToString(); con.Open(); SqlCommand cmd = con.CreateCommand(); StringBuilder sb = new StringBuilder(); sb.Append("select * from "); sb.Append("(select ROW_NUMBER() over(order by ArticleID desc) as row_number,T_Article.Title "); sb.Append("from T_Article ) t "); sb.Append("where row_number > (@PageIndex-1)*@PageCount "); sb.Append("and row_number <= (@PageIndex-1)*@PageCount + @PageCount"); cmd.CommandText = sb.ToString(); cmd.Parameters.Add(new SqlParameter("@PageIndex", PageIndex)); cmd.Parameters.Add(new SqlParameter("@PageCount", PageCount)); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } }
另外一个是获取数据的总数:GetTotalCount()
public int GetTotalCount() { using (SqlConnection con = new SqlConnection()) { con.ConnectionString = ConfigurationManager.ConnectionStrings["CollegeWebConnectionString"].ToString(); con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "select count(*) from T_Article"; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return Convert.ToInt32(ds.Tables[0].Rows[0][0]); } }
相关文章推荐
- [原创]将数据绑定至页面的两个Textbox控件上,并且可以通过按钮实现分页
- 一种处理分页控件和绑定数据方法
- asp.net中使用分页控件,并绑定数据到GridView或Repeater
- 把集合绑定到数据控件上并实现分页功能
- PagedDataSource数据绑定控件和AspNetPager分页控件结合使用列表分页
- 我的ASPxGridView控件的数据是在后台绑定的,但是实现不了筛选和分页的功能,只能显示一页的数据?
- 数据绑定控件[选择类型控件]、图像显示控件【哈哈、28号就可以回家啦、亲们回家过中秋节啦~!各位们都淡定点啊~!】
- 使用DataList控件绑定数据并实现分页
- Repeater控件绑定数据、分页、数据操作,最佳代码
- REPEATER控件显示数据,交替项显示;分页类的使用,Repeater控件分页;使用DataList控件绑定数据;使用DataList控件的选择模板
- Repeater控件绑定数据、分页、数据操作,最佳代码
- 数据绑定控件 简单分页
- .net中gridview控件数据绑定及分页
- Repeater控件数据绑定及分页
- Web页面折线图控件(可以直接绑定数据)
- VB.Net自己写的一个控件:ComboBox下拉列表中显示多列数据(可以绑定数据表)
- AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定
- ASP.NET中对DataList控件进行数据绑定并分页
- 【开源】分页数据绑定控件GridPager 1.0版发布
- ASP.NET中对DataList控件进行数据绑定并分页