分页:所有的数据绑定控件都可以用的

自己写了一个分页方法,数据绑定控件可以是Repeater、DataList、。。。等等都行,因为原理是每次重新获取页面所需的数据,然后再重新绑定。

后台代码:

  1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 BindData();
6 }
7 }
8
9 private void BindData()
10 {
11 Article a = new Article();
12 pageCount = 10; //设置每页记录条数
13 totalCount = a.GetTotalCount(); //获取记录总数
14 rpArticle.DataSource = a.GetPageData(GetCurrentIndex(), pageCount);//获取分页数据
15 rpArticle.DataBind();
16 ResetButtonStatus(pageCount,totalCount);
17 }
18
19 #region 分页处理
20
21 //使用例子
22 //private void BindData()
23 //{
24 // Article a = new Article();
25 // pageCount = 10; //设置每页记录条数
26 // totalCount = a.GetTotalCount(); //获取记录总数
27 // rpArticle.DataSource = a.GetPageData(GetCurrentIndex(), pageCount);//获取该页所需的数据
28 // rpArticle.DataBind();
29 // ResetButtonStatus(pageCount, totalCount);
30 //}
31
32 //获取分页数据SQL格式,参数@PageIndex:第几页数据,参数@PageCount:每页的记录条数
33 //sb.Append("select * from ");
34 //sb.Append("(select ROW_NUMBER() over(order by ArticleID desc) as row_number,T_Article.Title ");
35 //sb.Append("from T_Article ) t ");
36 //sb.Append("where row_number > (@PageIndex-1)*@PageCount ");
37 //sb.Append("and row_number <= (@PageIndex-1)*@PageCount + @PageCount");
38
39
40 /// <summary>
41 /// 每页的记录条数
42 /// </summary>
43 public int pageCount { get; set; }
44
45 /// <summary>
46 /// 记录总数
47 /// </summary>
48 public int totalCount { get; set; }
49
50 /// <summary>
51 /// 获取当前页
52 /// </summary>
53 /// <returns></returns>
54 private int GetCurrentIndex()
55 {
56 int index = 1;
57 if (ViewState["currentIndex"] == null)
58 {
59 ViewState["currentIndex"] = index;
60 }
61 else
62 {
63 index = Convert.ToInt32(ViewState["currentIndex"]);
64 }
65 return index;
66 }
67
68 /// <summary>
69 /// 重置按钮
70 /// </summary>
71 /// <param name="pageCount"></param>
72 private void ResetButtonStatus(int pageCount,int totalCount)
73 {
74 lblTotal.Text = totalCount.ToString();
75 int maxPage = totalCount % pageCount == 0 ? totalCount / pageCount : totalCount / pageCount + 1;
76 lblTotalPage.Text = maxPage.ToString();
77
78 ddlCurrentPage.Items.Clear();
79 for (int i = 1; i <= maxPage; i++)
80 {
81 ddlCurrentPage.Items.Add(new ListItem(i.ToString(), i.ToString()));
82 }
83 ddlCurrentPage.SelectedValue = GetCurrentIndex().ToString();
84 if (GetCurrentIndex() == maxPage)
85 {
86 lbtnNext.Enabled = false;
87 //lbtnNext.Text = "已到末页";
88 lbtnLast.Enabled = false;
89 }
90 else
91 {
92 lbtnNext.Enabled = true;
93 lbtnNext.Text = "下一页";
94 lbtnLast.Enabled = true;
95 }
96
97 if (GetCurrentIndex() == 1)
98 {
99 lbtnPrev.Enabled = false;
100 //lbtnPrev.Text = "已到首页";
101 lbtnFirst.Enabled = false;
102 }
103 else
104 {
105 lbtnPrev.Enabled = true;
106 lbtnPrev.Text = "上一页";
107 lbtnFirst.Enabled = true;
108 }
109
110 }
111
112 //上一页
113 protected void lbtnPrev_Click(object sender, EventArgs e)
114 {
115 ViewState["currentIndex"] = Convert.ToInt32(ViewState["currentIndex"]) - 1;
116 BindData();
117 }
118 //下一页
119 protected void lbtnNext_Click(object sender, EventArgs e)
120 {
121 ViewState["currentIndex"] = Convert.ToInt32(ViewState["currentIndex"]) + 1;
122 BindData();
123 }
124 //页面跳转
125 protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
126 {
127 ViewState["currentIndex"] = Convert.ToInt32(ddlCurrentPage.SelectedItem.Text);
128 BindData();
129 }
130 //首页
131 protected void lbtnFirst_Click(object sender, EventArgs e)
132 {
133 ViewState["currentIndex"] = 1;
134 BindData();
135 }
136 //尾页
137 protected void lbtnLast_Click(object sender, EventArgs e)
138 {
139 ViewState["currentIndex"] = Convert.ToInt32(lblTotalPage.Text);
140 BindData();
141 }
142 #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>
&nbsp;<asp:LinkButton ID="lbtnPrev" runat="server" OnClick="lbtnPrev_Click">上一页</asp:LinkButton>
&nbsp;<asp:LinkButton ID="lbtnNext" runat="server" OnClick="lbtnNext_Click">下一页</asp:LinkButton>
&nbsp;<asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">尾页</asp:LinkButton>
&nbsp;<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCurrentPage_SelectedIndexChanged">
</asp:DropDownList>
/<asp:Label ID="lblTotalPage" runat="server"></asp:Label>
&nbsp;总共
<asp:Label ID="lblTotal" runat="server"></asp:Label>
&nbsp;条记录</div>
</form>

  

效果图:

PS:

有人问Article类是干什么的,其实这个类只是封装了对我的数据表Article的操作而已,Article类我只写了两个方法,一个是获取分页数据GetPageData

 1 /// <summary>
2 /// 获取分页数据
3 /// </summary>
4 /// <param name="PageIndex">页索引</param>
5 /// <param name="PageCount">页大小</param>
6 /// <returns>DataSet</returns>
7 public DataSet GetPageData(int PageIndex, int PageCount)
8 {
9 using (SqlConnection con = new SqlConnection())
10 {
11 con.ConnectionString = ConfigurationManager.ConnectionStrings["CollegeWebConnectionString"].ToString();
12 con.Open();
13 SqlCommand cmd = con.CreateCommand();
14 StringBuilder sb = new StringBuilder();
15
16 sb.Append("select * from ");
17 sb.Append("(select ROW_NUMBER() over(order by ArticleID desc) as row_number,T_Article.Title ");
18 sb.Append("from T_Article ) t ");
19 sb.Append("where row_number > (@PageIndex-1)*@PageCount ");
20 sb.Append("and row_number <= (@PageIndex-1)*@PageCount + @PageCount");
21
22 cmd.CommandText = sb.ToString();
23 cmd.Parameters.Add(new SqlParameter("@PageIndex", PageIndex));
24 cmd.Parameters.Add(new SqlParameter("@PageCount", PageCount));
25
26 SqlDataAdapter da = new SqlDataAdapter(cmd);
27 DataSet ds = new DataSet();
28 da.Fill(ds);
29 return ds;
30 }
31 }

  另外一个是获取数据的总数:GetTotalCount()

 1         public int GetTotalCount()
2 {
3 using (SqlConnection con = new SqlConnection())
4 {
5 con.ConnectionString = ConfigurationManager.ConnectionStrings["CollegeWebConnectionString"].ToString();
6 con.Open();
7 SqlCommand cmd = con.CreateCommand();
8 cmd.CommandText = "select count(*) from T_Article";
9 SqlDataAdapter da = new SqlDataAdapter(cmd);
10 DataSet ds = new DataSet();
11 da.Fill(ds);
12 return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
13 }
14 }

  

posted @ 2011-07-15 11:06  魅猫之  阅读(...)  评论(... 编辑 收藏