asp.net数据绑定控件内置了分页功能,但是分页功能有限,样式不好控制,于是想自己开发分页控件
先晒晒图片效果 不要见笑哦
先晒晒前台页面分页代码前台用到的是linkbutton然后触发事件改变页码 绑定到下拉列表控件里面需要知道总页数pagedatasource的属性PageCount
View Code
<div id="showTfenye">
当前页:<asp:Label ID="lblPage" runat="server" Text="1"></asp:Label> 总页数:<asp:Label
ID="lblBackPage" runat="server" Text=""></asp:Label>
<asp:LinkButton ID="lbtnOne" runat="server" ForeColor="Red" OnClick="lbtnOne_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbtnUp" runat="server" ForeColor="Red" OnClick="lbtnUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnNext" runat="server" ForeColor="Red" OnClick="lbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnBack" runat="server" ForeColor="Red" OnClick="lbtnBack_Click">末页</asp:LinkButton>
<asp:DropDownList ID="DropDownList1" runat="server" Width="40px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" BackColor="#99FFCC" BorderColor="#3333FF"
BorderWidth="2px" OnClick="Button1_Click" Text="Go" />
</div>
当前页:<asp:Label ID="lblPage" runat="server" Text="1"></asp:Label> 总页数:<asp:Label
ID="lblBackPage" runat="server" Text=""></asp:Label>
<asp:LinkButton ID="lbtnOne" runat="server" ForeColor="Red" OnClick="lbtnOne_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbtnUp" runat="server" ForeColor="Red" OnClick="lbtnUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnNext" runat="server" ForeColor="Red" OnClick="lbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnBack" runat="server" ForeColor="Red" OnClick="lbtnBack_Click">末页</asp:LinkButton>
<asp:DropDownList ID="DropDownList1" runat="server" Width="40px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" BackColor="#99FFCC" BorderColor="#3333FF"
BorderWidth="2px" OnClick="Button1_Click" Text="Go" />
</div>
这个分页我是封装在用户控件里面,用pagedatasource作为中间中间数据源,然后传给数据绑定控件比如,GridView
重要的利用pageDataSource的
ps.DataSource = dt.DefaultView;
//是否可以分页
ps.AllowPaging = true;
//显示分页的数量
ps.PageSize = this.ShowCount;
//取得当前页的页码
ps.CurrentPageIndex = curPage - 1;
//显示分页数量
this.lblBackPage.Text = Convert.ToString(ps.PageCount);
View Code
1 using System;
2 using System.Data;
3 using System.Web.UI.WebControls;
4 namespace Pager.controls
5 {
6 public partial class pager1 : System.Web.UI.UserControl
7 {
8 protected void Page_Load(object sender, EventArgs e)
9 {
10 if (!IsPostBack)
11 {
12 bind();
13 showPage();
14 }
15 }
16 #region 定义相关属性
17
18 private DataBoundControl dataControl;
19 public DataBoundControl DataControl
20 {
21 get { return this.dataControl; }
22 set { this.dataControl = value; }
23 }
24 #region 其他数据源控件
25
26 //private BaseDataBoundControl dataControl;//数据源控件类型
27 /// <summary>
28 /// 数据绑定控件
29 /// </summary>
30 //public BaseDataBoundControl DataControl
31 //{
32 // get { return this.dataControl; }
33 // set { this.dataControl = value; }
34 //}
35 //DataList控件
36 //private DataList dataControl;
37 //public DataList DataControl
38 //{
39 // get { return this.dataControl; }
40 // set { this.dataControl = value; }
41 //}
42 //repeater控件
43 //private Repeater dataControl;
44 //public Repeater DataControl
45 //{
46 // get { return this.dataControl; }
47 // set { this.dataControl = value; }
48 //}
49 #endregion
50 private int showCount;//每页数量
51 /// <summary>
52 /// 每页显示的数量
53 /// </summary>
54 public int ShowCount
55 {
56 get { return this.showCount; }
57 set { this.showCount = value; }
58 }
59 private DataTable objSource;//数据源
60 /// <summary>
61 /// DataTable类型的数据源
62 /// </summary>
63 public DataTable Objsource
64 {
65 get { return this.objSource; }
66 set { this.objSource = value; }
67 }
68 #endregion
69
70 #region 分页方法
71 #region 分页
72 /// <summary>
73 /// 分页
74 /// </summary>
75 protected void bind()
76 {
77 //获得当前的页码
78 int curPage = Convert.ToInt32(this.lblPage.Text);
79 //使用PageDataSource类实现数据控件的分页
80 PagedDataSource ps = new PagedDataSource();
81 //获取数据源
82 DataTable dt = new DataTable();
83 dt = this.objSource;
84 ps.DataSource = dt.DefaultView;
85 //是否可以分页
86 ps.AllowPaging = true;
87 //显示分页的数量
88 ps.PageSize = this.ShowCount;
89 //取得当前页的页码
90 ps.CurrentPageIndex = curPage - 1;
91 this.lbtnUp.Enabled = true;
92 this.lbtnNext.Enabled = true;
93 this.lbtnBack.Enabled = true;
94 this.lbtnOne.Enabled = true;
95 if (curPage == 1)
96 {
97 //不显示第一页按钮
98 this.lbtnOne.Enabled = false;
99 //不显示上一页按钮
100 this.lbtnUp.Enabled = false;
101 }
102 if (curPage == ps.PageCount)
103 {
104 //不显示下一页按钮
105 this.lbtnNext.Enabled = false;
106 //不显示最后一页按钮
107 this.lbtnBack.Enabled = false;
108 }
109 //显示分页数量
110 this.lblBackPage.Text = Convert.ToString(ps.PageCount);
111 //绑定到数据源控件
112 this.DataControl.DataSource = ps;
113 this.DataControl.DataBind();
114 }
115 #endregion
116 #region 第一页
117 /// <summary>
118 /// 第一页
119 /// </summary>
120 /// <param name="sender"></param>
121 /// <param name="e"></param>
122 protected void lbtnOne_Click(object sender, EventArgs e)
123 {
124 this.lblPage.Text = "1";
125 this.bind();
126 }
127 #endregion
128 #region 上一页
129 /// <summary>
130 /// 上一页
131 /// </summary>
132 /// <param name="sender"></param>
133 /// <param name="e"></param>
134 protected void lbtnUp_Click(object sender, EventArgs e)
135 {
136 this.lblPage.Text = Convert.ToString(Convert.ToInt32(this.lblPage.Text) - 1);
137 this.bind();
138 }
139 #endregion
140 #region 下一页
141 /// <summary>
142 /// 下一页
143 /// </summary>
144 /// <param name="sender"></param>
145 /// <param name="e"></param>
146 protected void lbtnNext_Click(object sender, EventArgs e)
147 {
148 this.lblPage.Text = Convert.ToString(Convert.ToInt32(this.lblPage.Text) + 1);
149 this.bind();
150 }
151 #endregion
152 #region 最后一页
153 /// <summary>
154 /// 最后一页
155 /// </summary>
156 /// <param name="sender"></param>
157 /// <param name="e"></param>
158 protected void lbtnBack_Click(object sender, EventArgs e)
159 {
160 this.lblPage.Text = this.lblBackPage.Text;
161 this.bind();
162 }
163 #endregion
164 //显示所有页数
165 public void showPage()
166 {
167 int count = Convert.ToInt32(lblBackPage.Text);
168 int[] num = new int[count];
169 for (int i = 1; i <= count; i++)
170 {
171 num[i - 1] = i;
172 //DropDownList1.DataValueField = i.ToString();//指定下拉列表中的值部分
173 }
174 DropDownList1.DataSource = num;
175 DropDownList1.DataBind();
176 }
177 //跳转到指定页
178 protected void Button1_Click(object sender, EventArgs e)
179 {
180 string page = DropDownList1.SelectedValue;
181 lblPage.Text = page;//由于显示当前页控件从1开始
182 bind();
183 }
184 #endregion
185 }
186 }
2 using System.Data;
3 using System.Web.UI.WebControls;
4 namespace Pager.controls
5 {
6 public partial class pager1 : System.Web.UI.UserControl
7 {
8 protected void Page_Load(object sender, EventArgs e)
9 {
10 if (!IsPostBack)
11 {
12 bind();
13 showPage();
14 }
15 }
16 #region 定义相关属性
17
18 private DataBoundControl dataControl;
19 public DataBoundControl DataControl
20 {
21 get { return this.dataControl; }
22 set { this.dataControl = value; }
23 }
24 #region 其他数据源控件
25
26 //private BaseDataBoundControl dataControl;//数据源控件类型
27 /// <summary>
28 /// 数据绑定控件
29 /// </summary>
30 //public BaseDataBoundControl DataControl
31 //{
32 // get { return this.dataControl; }
33 // set { this.dataControl = value; }
34 //}
35 //DataList控件
36 //private DataList dataControl;
37 //public DataList DataControl
38 //{
39 // get { return this.dataControl; }
40 // set { this.dataControl = value; }
41 //}
42 //repeater控件
43 //private Repeater dataControl;
44 //public Repeater DataControl
45 //{
46 // get { return this.dataControl; }
47 // set { this.dataControl = value; }
48 //}
49 #endregion
50 private int showCount;//每页数量
51 /// <summary>
52 /// 每页显示的数量
53 /// </summary>
54 public int ShowCount
55 {
56 get { return this.showCount; }
57 set { this.showCount = value; }
58 }
59 private DataTable objSource;//数据源
60 /// <summary>
61 /// DataTable类型的数据源
62 /// </summary>
63 public DataTable Objsource
64 {
65 get { return this.objSource; }
66 set { this.objSource = value; }
67 }
68 #endregion
69
70 #region 分页方法
71 #region 分页
72 /// <summary>
73 /// 分页
74 /// </summary>
75 protected void bind()
76 {
77 //获得当前的页码
78 int curPage = Convert.ToInt32(this.lblPage.Text);
79 //使用PageDataSource类实现数据控件的分页
80 PagedDataSource ps = new PagedDataSource();
81 //获取数据源
82 DataTable dt = new DataTable();
83 dt = this.objSource;
84 ps.DataSource = dt.DefaultView;
85 //是否可以分页
86 ps.AllowPaging = true;
87 //显示分页的数量
88 ps.PageSize = this.ShowCount;
89 //取得当前页的页码
90 ps.CurrentPageIndex = curPage - 1;
91 this.lbtnUp.Enabled = true;
92 this.lbtnNext.Enabled = true;
93 this.lbtnBack.Enabled = true;
94 this.lbtnOne.Enabled = true;
95 if (curPage == 1)
96 {
97 //不显示第一页按钮
98 this.lbtnOne.Enabled = false;
99 //不显示上一页按钮
100 this.lbtnUp.Enabled = false;
101 }
102 if (curPage == ps.PageCount)
103 {
104 //不显示下一页按钮
105 this.lbtnNext.Enabled = false;
106 //不显示最后一页按钮
107 this.lbtnBack.Enabled = false;
108 }
109 //显示分页数量
110 this.lblBackPage.Text = Convert.ToString(ps.PageCount);
111 //绑定到数据源控件
112 this.DataControl.DataSource = ps;
113 this.DataControl.DataBind();
114 }
115 #endregion
116 #region 第一页
117 /// <summary>
118 /// 第一页
119 /// </summary>
120 /// <param name="sender"></param>
121 /// <param name="e"></param>
122 protected void lbtnOne_Click(object sender, EventArgs e)
123 {
124 this.lblPage.Text = "1";
125 this.bind();
126 }
127 #endregion
128 #region 上一页
129 /// <summary>
130 /// 上一页
131 /// </summary>
132 /// <param name="sender"></param>
133 /// <param name="e"></param>
134 protected void lbtnUp_Click(object sender, EventArgs e)
135 {
136 this.lblPage.Text = Convert.ToString(Convert.ToInt32(this.lblPage.Text) - 1);
137 this.bind();
138 }
139 #endregion
140 #region 下一页
141 /// <summary>
142 /// 下一页
143 /// </summary>
144 /// <param name="sender"></param>
145 /// <param name="e"></param>
146 protected void lbtnNext_Click(object sender, EventArgs e)
147 {
148 this.lblPage.Text = Convert.ToString(Convert.ToInt32(this.lblPage.Text) + 1);
149 this.bind();
150 }
151 #endregion
152 #region 最后一页
153 /// <summary>
154 /// 最后一页
155 /// </summary>
156 /// <param name="sender"></param>
157 /// <param name="e"></param>
158 protected void lbtnBack_Click(object sender, EventArgs e)
159 {
160 this.lblPage.Text = this.lblBackPage.Text;
161 this.bind();
162 }
163 #endregion
164 //显示所有页数
165 public void showPage()
166 {
167 int count = Convert.ToInt32(lblBackPage.Text);
168 int[] num = new int[count];
169 for (int i = 1; i <= count; i++)
170 {
171 num[i - 1] = i;
172 //DropDownList1.DataValueField = i.ToString();//指定下拉列表中的值部分
173 }
174 DropDownList1.DataSource = num;
175 DropDownList1.DataBind();
176 }
177 //跳转到指定页
178 protected void Button1_Click(object sender, EventArgs e)
179 {
180 string page = DropDownList1.SelectedValue;
181 lblPage.Text = page;//由于显示当前页控件从1开始
182 bind();
183 }
184 #endregion
185 }
186 }
调用时 需要给属性 数据源控件DataControl,每页显示数量ShowCount,数据源Objsource为DataTable类型
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 //gridview的分页
4 this.pager11.DataControl = this.GridView1;
5 this.pager11.ShowCount = 20;
6 sqlHelper s = new sqlHelper();
7 this.pager11.Objsource =s.getAll().Tables[0];
8 }
2 {
3 //gridview的分页
4 this.pager11.DataControl = this.GridView1;
5 this.pager11.ShowCount = 20;
6 sqlHelper s = new sqlHelper();
7 this.pager11.Objsource =s.getAll().Tables[0];
8 }
其中注意一点是 调用时代码不能写在 Ispostback里面
代码在下一页提供,欢迎期待……