使用简单的分页控件,有页码显示、上页、下页,简单修改就可以增加跳转到多页的功能。不修改URL地址。
常看圆子里面的网友说分页控件。也有很多人在网上找一个好用的分页控件。我把自己做的放一个上来给大家吧。其实东西蛮简单的,看懂了思路就可以动手写一个了。
废话不多说。先表明需求:
- 能分页(废话)
- 能够在禁用viewstate的情况下也可以正常使用
- 代码简单
- 不修改地址栏
要达到需求中的第2和第4条就需要一些辅助对象。因为不使用viewstate那么就需要使用能够保存当前页面的对象,TextBox、Literal和Label都可以实现。为了简化控件里面的控件对象,特别是页码对象,则使用直接Html代码的方式,通过a标签中的href属性对页码的文本框内容修改,然后引发postback以把分页结果返回到服务器端,具体实现偷了懒,直接用了 pageNumLinkString 变量。其实“首页”、“上一页”、“下一页”和“末页”也可以用同样的思路来实现。单修改就可以增加跳转到某页的功能(把俩个控件的style属性去了就可以了)。
分页的用户控件的页面代码:
用户控件代码
1<asp:Panel ID="PageDiv" CssClass="SplitPage" runat="server" Width="100%" HorizontalAlign="Right">
2 <asp:LinkButton ID="lkbtnFirst" runat="server" OnClick="lkbtnFirst_Click">首页</asp:LinkButton>
3 <asp:LinkButton ID="lkbtnPre" runat="server" OnClick="lkbtnPre_Click">上一页</asp:LinkButton>
4 <asp:Literal runat="server" ID="lbPageNum"></asp:Literal>
5 <asp:TextBox ID="tbPageNum" runat="server" Style="display: none;"></asp:TextBox>
6 <asp:Button ID="btnSplitPostBack" runat="server" OnClick="btnSplitPostBack_Click" Style="display: none;" UseSubmitBehavior="false" />
7 <asp:LinkButton ID="lkbtnNext" runat="server" OnClick="lkbtnNext_Click">下一页</asp:LinkButton>
8 <asp:LinkButton ID="lkbtnLast" runat="server" OnClick="lkbtnLast_Click">末页</asp:LinkButton>
9 <asp:Literal ID="ltSplitPageInfo" runat="server"></asp:Literal>
10 <asp:TextBox ID="tbCurrentPageNum" runat="server" Style="display: none;"></asp:TextBox>
11</asp:Panel>
1<asp:Panel ID="PageDiv" CssClass="SplitPage" runat="server" Width="100%" HorizontalAlign="Right">
2 <asp:LinkButton ID="lkbtnFirst" runat="server" OnClick="lkbtnFirst_Click">首页</asp:LinkButton>
3 <asp:LinkButton ID="lkbtnPre" runat="server" OnClick="lkbtnPre_Click">上一页</asp:LinkButton>
4 <asp:Literal runat="server" ID="lbPageNum"></asp:Literal>
5 <asp:TextBox ID="tbPageNum" runat="server" Style="display: none;"></asp:TextBox>
6 <asp:Button ID="btnSplitPostBack" runat="server" OnClick="btnSplitPostBack_Click" Style="display: none;" UseSubmitBehavior="false" />
7 <asp:LinkButton ID="lkbtnNext" runat="server" OnClick="lkbtnNext_Click">下一页</asp:LinkButton>
8 <asp:LinkButton ID="lkbtnLast" runat="server" OnClick="lkbtnLast_Click">末页</asp:LinkButton>
9 <asp:Literal ID="ltSplitPageInfo" runat="server"></asp:Literal>
10 <asp:TextBox ID="tbCurrentPageNum" runat="server" Style="display: none;"></asp:TextBox>
11</asp:Panel>
对应代码页代码:
页面代码
1public partial class SplitPage : System.Web.UI.UserControl
2{
3 /**//// <summary>
4 /// 分页事件
5 /// </summary>
6 /// <param name="sender">SplitPageControl对象</param>
7 /// <param name="e">分页参数</param>
8 /// <returns></returns>
9 [Description("分页")]
10 public event EventHandler<SplitEventArgs> SplitPaging(object sender ,SplitEventArgs e);
11
12 /**//// <summary>
13 /// 页码链接
14 /// </summary>
15 private static string pageNumLinkString = " <a href=\"javascript:document.getElementById('{0}').value={1};__doPostBack('{2}','');\" class=\"SplitPageLink\">{1}</a>";
16
17 /**//// <summary>
18 /// 每頁顯示紀錄數
19 /// </summary>
20 public int PerPageShowCount
21 {
22 get
23 {
24 object obj = ViewState["perPageShowCount"];
25 if (obj == null)
26 return 10;
27 return (int)obj;
28 }
29 set
30 {
31 if (value < 0)
32 value = 10;
33 ViewState["perPageShowCount"] = value;
34 }
35 }
36
37 /**//// <summary>
38 /// 设置或获取当前记录的页码位置
39 /// </summary>
40 public int PageNum
41 {
42 get
43 {
44 return int.Parse(tbCurrentPageNum.Text);
45 }
46 set
47 {
48 if (value < 0)
49 value = 0;
50 tbCurrentPageNum.Text = value.ToString();
51 }
52 }
53
54 /**//// <summary>
55 /// 设置或获取当前记录的總頁碼數
56 /// </summary>
57 public int MaxPageNum
58 {
59 get
60 {
61 object obj = ViewState["MaxPageNum"];
62 if (obj == null)
63 return 1;
64 return (int)obj;
65 }
66 set
67 {
68 if (value <= 0)
69 value = 1;
70 ViewState["MaxPageNum"] = value;
71 }
72 }
73
74 /**//// <summary>
75 /// 设置或获取是否显示页码
76 /// </summary>
77 public bool ShowPageNum
78 {
79 get
80 {
81 object obj = ViewState["ShowPageNum"];
82 if (obj == null)
83 return false;
84 return (bool)obj;
85 }
86 set
87 {
88 ViewState["ShowPageNum"] = value;
89 }
90 }
91
92 /**//// <summary>
93 /// 内部使用分页
94 /// </summary>
95 /// <param name="currentPage">当前页码</param>
96 /// <param name="perPageShowCount">每页显示多少条页码</param>
97 private void SetSplitPage(int currentPage, int perPageShowCount)
98 {
99 OnSplitPaging(new SplitEventArgs(currentPage, perPageShowCount));
100 }
101
102 /**//// <summary>
103 /// 引发分页事件
104 /// </summary>
105 /// <param name="loadDataInfo">加載數據用函數</param>
106 protected virtual void OnSplitPaging(SplitEventArgs e)
107 {
108 if (SplitPaging != null)
109 SplitPaging(this, e);
110 }
111
112 /**//// <summary>
113 /// 分页控制
114 /// </summary>
115 /// <param name="pageNum">当前页码</param>
116 /// <param name="sp">分页对象</param>
117 public void SetSplitPageInfo(int currentPageNum, int PageCount)
118 {
119 PageNum = currentPageNum;
120 MaxPageNum = PageCount;
121 ltSplitPageInfo.Text = string.Format("第{0}页,共{1}页,每页{2}行", (PageNum + 1), MaxPageNum, PerPageShowCount);
122 if (MaxPageNum > 1)//页码多余一页 分页控制判断
123 {
124 if (PageNum == 0)//在首页
125 {
126 lkbtnFirst.Enabled = false;
127 lkbtnLast.Enabled = true;
128 lkbtnNext.Enabled = true;
129 lkbtnPre.Enabled = false;
130 }
131 else if (PageNum == (MaxPageNum - 1))//在末页
132 {
133 lkbtnFirst.Enabled = true;
134 lkbtnLast.Enabled = false;
135 lkbtnNext.Enabled = false;
136 lkbtnPre.Enabled = true;
137 }
138 else if (PageNum > (MaxPageNum - 1))//当前页大于页码总数
139 {
140 lkbtnFirst.Enabled = false;
141 lkbtnLast.Enabled = false;
142 lkbtnNext.Enabled = false;
143 lkbtnPre.Enabled = false;
144 }
145 else//在其他页码
146 {
147 lkbtnFirst.Enabled = true;
148 lkbtnLast.Enabled = true;
149 lkbtnNext.Enabled = true;
150 lkbtnPre.Enabled = true;
151 }
152 }
153 else//只有一页
154 {
155 lkbtnFirst.Enabled = false;
156 lkbtnLast.Enabled = false;
157 lkbtnNext.Enabled = false;
158 lkbtnPre.Enabled = false;
159 }
160
161 if (ShowPageNum)
162 {
163 StringBuilder sb = new StringBuilder();
164
165 //定义要显示的页码最大于最小值
166 int pageStart = Math.Max(pageNum - 3, Math.Min(MaxPageNum - 8, 1));
167 int pageEnd = Math.Min(MaxPageNum, pageStart + 8);
168 pageStart = (pageEnd - 8 > 0) ? (pageEnd - 8) : 1;
169
170 for (int i = pageStart; i <= PageNum; i++)
171 {
172 sb.AppendFormat(pageNumLinkString, tbPageNum.ClientID, i, btnSplitPostBack.ClientID.Replace('_', '$'));
173 }
174 sb.AppendFormat("{0}", PageNum + 1);
175 for (int i = PageNum + 2; i <= pageEnd; i++)
176 {
177 sb.AppendFormat(pageNumLinkString, tbPageNum.ClientID, i, btnSplitPostBack.ClientID.Replace('_', '$'));
178 }
179
180 lbPageNum.Text = sb.ToString();
181 }
182 }
183
184 /**//// <summary>
185 /// 首页
186 /// </summary>
187 /// <param name="sender"></param>
188 /// <param name="e"></param>
189 protected void lkbtnFirst_Click(object sender, EventArgs e)
190 {
191 SetSplitPage(0, PerPageShowCount);
192 }
193
194 /**//// <summary>
195 /// 上一页
196 /// </summary>
197 /// <param name="sender"></param>
198 /// <param name="e"></param>
199 protected void lkbtnPre_Click(object sender, EventArgs e)
200 {
201 SetSplitPage(PageNum - 1, PerPageShowCount);
202 }
203 /**//// <summary>
204 /// 下一页
205 /// </summary>
206 /// <param name="sender"></param>
207 /// <param name="e"></param>
208 protected void lkbtnNext_Click(object sender, EventArgs e)
209 {
210 SetSplitPage(PageNum + 1, PerPageShowCount);
211 }
212 /**//// <summary>
213 /// 末页
214 /// </summary>
215 /// <param name="sender"></param>
216 /// <param name="e"></param>
217 protected void lkbtnLast_Click(object sender, EventArgs e)
218 {
219 SetSplitPage(MaxPageNum - 1, PerPageShowCount);
220 }
221
222 /**//// <summary>
223 /// 提交信息
224 /// </summary>
225 /// <param name="sender"></param>
226 /// <param name="e"></param>
227 protected void btnSplitPostBack_Click(object sender, EventArgs e)
228 {
229 SetSplitPage(int.Parse(tbPageNum.Text) - 1, PerPageShowCount);
230 }
231}
232
1public partial class SplitPage : System.Web.UI.UserControl
2{
3 /**//// <summary>
4 /// 分页事件
5 /// </summary>
6 /// <param name="sender">SplitPageControl对象</param>
7 /// <param name="e">分页参数</param>
8 /// <returns></returns>
9 [Description("分页")]
10 public event EventHandler<SplitEventArgs> SplitPaging(object sender ,SplitEventArgs e);
11
12 /**//// <summary>
13 /// 页码链接
14 /// </summary>
15 private static string pageNumLinkString = " <a href=\"javascript:document.getElementById('{0}').value={1};__doPostBack('{2}','');\" class=\"SplitPageLink\">{1}</a>";
16
17 /**//// <summary>
18 /// 每頁顯示紀錄數
19 /// </summary>
20 public int PerPageShowCount
21 {
22 get
23 {
24 object obj = ViewState["perPageShowCount"];
25 if (obj == null)
26 return 10;
27 return (int)obj;
28 }
29 set
30 {
31 if (value < 0)
32 value = 10;
33 ViewState["perPageShowCount"] = value;
34 }
35 }
36
37 /**//// <summary>
38 /// 设置或获取当前记录的页码位置
39 /// </summary>
40 public int PageNum
41 {
42 get
43 {
44 return int.Parse(tbCurrentPageNum.Text);
45 }
46 set
47 {
48 if (value < 0)
49 value = 0;
50 tbCurrentPageNum.Text = value.ToString();
51 }
52 }
53
54 /**//// <summary>
55 /// 设置或获取当前记录的總頁碼數
56 /// </summary>
57 public int MaxPageNum
58 {
59 get
60 {
61 object obj = ViewState["MaxPageNum"];
62 if (obj == null)
63 return 1;
64 return (int)obj;
65 }
66 set
67 {
68 if (value <= 0)
69 value = 1;
70 ViewState["MaxPageNum"] = value;
71 }
72 }
73
74 /**//// <summary>
75 /// 设置或获取是否显示页码
76 /// </summary>
77 public bool ShowPageNum
78 {
79 get
80 {
81 object obj = ViewState["ShowPageNum"];
82 if (obj == null)
83 return false;
84 return (bool)obj;
85 }
86 set
87 {
88 ViewState["ShowPageNum"] = value;
89 }
90 }
91
92 /**//// <summary>
93 /// 内部使用分页
94 /// </summary>
95 /// <param name="currentPage">当前页码</param>
96 /// <param name="perPageShowCount">每页显示多少条页码</param>
97 private void SetSplitPage(int currentPage, int perPageShowCount)
98 {
99 OnSplitPaging(new SplitEventArgs(currentPage, perPageShowCount));
100 }
101
102 /**//// <summary>
103 /// 引发分页事件
104 /// </summary>
105 /// <param name="loadDataInfo">加載數據用函數</param>
106 protected virtual void OnSplitPaging(SplitEventArgs e)
107 {
108 if (SplitPaging != null)
109 SplitPaging(this, e);
110 }
111
112 /**//// <summary>
113 /// 分页控制
114 /// </summary>
115 /// <param name="pageNum">当前页码</param>
116 /// <param name="sp">分页对象</param>
117 public void SetSplitPageInfo(int currentPageNum, int PageCount)
118 {
119 PageNum = currentPageNum;
120 MaxPageNum = PageCount;
121 ltSplitPageInfo.Text = string.Format("第{0}页,共{1}页,每页{2}行", (PageNum + 1), MaxPageNum, PerPageShowCount);
122 if (MaxPageNum > 1)//页码多余一页 分页控制判断
123 {
124 if (PageNum == 0)//在首页
125 {
126 lkbtnFirst.Enabled = false;
127 lkbtnLast.Enabled = true;
128 lkbtnNext.Enabled = true;
129 lkbtnPre.Enabled = false;
130 }
131 else if (PageNum == (MaxPageNum - 1))//在末页
132 {
133 lkbtnFirst.Enabled = true;
134 lkbtnLast.Enabled = false;
135 lkbtnNext.Enabled = false;
136 lkbtnPre.Enabled = true;
137 }
138 else if (PageNum > (MaxPageNum - 1))//当前页大于页码总数
139 {
140 lkbtnFirst.Enabled = false;
141 lkbtnLast.Enabled = false;
142 lkbtnNext.Enabled = false;
143 lkbtnPre.Enabled = false;
144 }
145 else//在其他页码
146 {
147 lkbtnFirst.Enabled = true;
148 lkbtnLast.Enabled = true;
149 lkbtnNext.Enabled = true;
150 lkbtnPre.Enabled = true;
151 }
152 }
153 else//只有一页
154 {
155 lkbtnFirst.Enabled = false;
156 lkbtnLast.Enabled = false;
157 lkbtnNext.Enabled = false;
158 lkbtnPre.Enabled = false;
159 }
160
161 if (ShowPageNum)
162 {
163 StringBuilder sb = new StringBuilder();
164
165 //定义要显示的页码最大于最小值
166 int pageStart = Math.Max(pageNum - 3, Math.Min(MaxPageNum - 8, 1));
167 int pageEnd = Math.Min(MaxPageNum, pageStart + 8);
168 pageStart = (pageEnd - 8 > 0) ? (pageEnd - 8) : 1;
169
170 for (int i = pageStart; i <= PageNum; i++)
171 {
172 sb.AppendFormat(pageNumLinkString, tbPageNum.ClientID, i, btnSplitPostBack.ClientID.Replace('_', '$'));
173 }
174 sb.AppendFormat("{0}", PageNum + 1);
175 for (int i = PageNum + 2; i <= pageEnd; i++)
176 {
177 sb.AppendFormat(pageNumLinkString, tbPageNum.ClientID, i, btnSplitPostBack.ClientID.Replace('_', '$'));
178 }
179
180 lbPageNum.Text = sb.ToString();
181 }
182 }
183
184 /**//// <summary>
185 /// 首页
186 /// </summary>
187 /// <param name="sender"></param>
188 /// <param name="e"></param>
189 protected void lkbtnFirst_Click(object sender, EventArgs e)
190 {
191 SetSplitPage(0, PerPageShowCount);
192 }
193
194 /**//// <summary>
195 /// 上一页
196 /// </summary>
197 /// <param name="sender"></param>
198 /// <param name="e"></param>
199 protected void lkbtnPre_Click(object sender, EventArgs e)
200 {
201 SetSplitPage(PageNum - 1, PerPageShowCount);
202 }
203 /**//// <summary>
204 /// 下一页
205 /// </summary>
206 /// <param name="sender"></param>
207 /// <param name="e"></param>
208 protected void lkbtnNext_Click(object sender, EventArgs e)
209 {
210 SetSplitPage(PageNum + 1, PerPageShowCount);
211 }
212 /**//// <summary>
213 /// 末页
214 /// </summary>
215 /// <param name="sender"></param>
216 /// <param name="e"></param>
217 protected void lkbtnLast_Click(object sender, EventArgs e)
218 {
219 SetSplitPage(MaxPageNum - 1, PerPageShowCount);
220 }
221
222 /**//// <summary>
223 /// 提交信息
224 /// </summary>
225 /// <param name="sender"></param>
226 /// <param name="e"></param>
227 protected void btnSplitPostBack_Click(object sender, EventArgs e)
228 {
229 SetSplitPage(int.Parse(tbPageNum.Text) - 1, PerPageShowCount);
230 }
231}
232
使用的时候,把这个放到页面中,在页面的Page_load事件中加载事件中就可以了
调用代码
1protected void Page_Load(object sender, EventArgs e)
2 {
3 SplitPage1.SplitPaging += new EventHandler<SplitEventArgs>(SplitPage1_SplitPaging);
4 }
5//页面回调事件
6void SplitPage1_SplitPaging(object sender, WebSite.Admin.Control.SplitEventArgs e)
7 {
8 LoadDataInfo(e.CurrentPage, e.PerPageShowCount);
9 //设置分页相关信息
10 SplitPage1.SetSplitPageInfo(newPageNum, PageCount);
11 }
1protected void Page_Load(object sender, EventArgs e)
2 {
3 SplitPage1.SplitPaging += new EventHandler<SplitEventArgs>(SplitPage1_SplitPaging);
4 }
5//页面回调事件
6void SplitPage1_SplitPaging(object sender, WebSite.Admin.Control.SplitEventArgs e)
7 {
8 LoadDataInfo(e.CurrentPage, e.PerPageShowCount);
9 //设置分页相关信息
10 SplitPage1.SetSplitPageInfo(newPageNum, PageCount);
11 }
中国特色没有代码下载。