-
gentle做的分页控件
1、自己定义控件本身
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6using System.Web.UI.HtmlControls;
7using System.Drawing;
8using System.Collections;
9using System.Collections.Specialized;
10using System.ComponentModel;
11using System.Data;
12
13using Gentle.Framework;
14/**//// -----------------------------------------------------------------------------
15/// Project : CtrolBox
16/// Class : PagerCtl///
17/// -----------------------------------------------------------------------------
18/// <summary>
19/// gentle分页
20/// </summary>
21/// <remarks>
22/// </remarks>
23/// <history>
24/// xx. YYYY/MM/DD VER AUTHOR COMMENTS
25/// 1. 2007/2/2 1.00 susan CREATE
26/// </history>
27/// ----------------------------------------------------------------------------- 28
29namespace CtrolBox
30{
31 [assembly: WebResource(CtrolBox.PagerCtl.CSSResName, "text/css")]
32 [assembly: WebResource(CtrolBox.PagerCtl.JSResName, "text/javascript")]
33
34 public class PagerCtl: WebControl, INamingContainer, IPostBackDataHandler
35 {
36 内部属性#region 内部属性
37
38 public const string CSSResName = "CtrolBox.Calendar.css";
39 public const string JSResName = "CtrolBox.Calendar.js";
40 static string strCssLink;
41 static string strJsLink;
42
43 private TextBox _EditBox = new TextBox();
44 private Label _strCount = new Label();
45 private DataTable _DataSource = new DataTable();
46 private int _PageSize = 10;
47
48 #endregion
49
50 属性#region 属性
51
52 [Bindable(true),
53 Category("Appearance"),
54 DefaultValue("0"),
55 Description("每页资料数")]
56 public int PageSize
57 {
58 get
59 {
60 return _PageSize;
61 }
62 set
63 {
64 _PageSize = value;
65 }
66 }
67
68 [Bindable(true),
69 Category("Appearance"),
70 DefaultValue(""),
71 Description("显示框的值")]
72 public string PageValue
73 {
74 get
75 {
76 return _EditBox.Text;
77 }
78 set
79 {
80 _EditBox.Text = value;
81 }
82 }
83
84 [Bindable(true),
85 Category("Appearance"),
86 DefaultValue(""),
87 Description("sql")]
88 public string Sql
89 {
90 get
91 {
92 if (ViewState["Sql"] == null)
93 return string.Empty;
94 return Convert.ToString(ViewState["Sql"]);
95
96 }
97 set
98 {
99 ViewState["Sql"] = value;
100 setPageload();
101 }
102 }
103
104 [Bindable(true),
105 Category("Appearance"),
106 DefaultValue(""),
107 Description("取得信息值")]
108 public DataTable DataSource
109 {
110 get
111 {
112 return _DataSource;
113 }
114 }
115
116 #endregion
117
118 公开事件#region 公开事件
119
120 [Bindable(true),
121 Category("Appearance"),
122 DefaultValue(""),
123 Description("事件")]
124 public event System.EventHandler btnClick;
125
126 /**//// <summary>
127 /// 第一页
128 /// </summary>
129 /// <param name="Sender"></param>
130 /// <param name="e"></param>
131 public void First_Click(Object Sender, EventArgs e)
132 {
133 this._EditBox.Text = "1";
134 btn_Click(Sender, e);
135 }
136
137 /**//// <summary>
138 /// 下一面
139 /// </summary>
140 /// <param name="Sender"></param>
141 /// <param name="e"></param>
142 public void Next_Click(Object Sender, EventArgs e)
143 {
144 this._EditBox.Text = Convert.ToString(Convert.ToInt32(this._EditBox.Text) + 1);
145 btn_Click(Sender, e);
146 }
147
148 /**//// <summary>
149 /// 上一页
150 /// </summary>
151 /// <param name="Sender"></param>
152 /// <param name="e"></param>
153 public void Pre_Click(Object Sender, EventArgs e)
154 {
155 this._EditBox.Text = Convert.ToString(Convert.ToInt32(this._EditBox.Text) - 1);
156 btn_Click(Sender, e);
157 }
158
159 /**//// <summary>
160 /// 最后一页
161 /// </summary>
162 /// <param name="Sender"></param>
163 /// <param name="e"></param>
164 public void Last_Click(Object Sender, EventArgs e)
165 {
166 this._EditBox.Text = fn_getPages();
167 btn_Click(Sender, e);
168 }
169
170 /**//// <summary>
171 /// 到何页
172 /// </summary>
173 /// <param name="Sender"></param>
174 /// <param name="e"></param>
175 public void btn_Click(Object Sender, EventArgs e)
176 {
177 GentleSqlFactory sqlfact = Broker.GetSqlFactory();
178 IDbCommand cmd = sqlfact.GetCommand();
179
180 SqlStatement st = null;
181 SqlResult sr = null;
182 if (this._EditBox.Text.Trim() != "")
183 {
184 st = new SqlStatement(Gentle.Framework.StatementType.Select, cmd, ViewState["Sql"].ToString(), this._PageSize, this._PageSize * (Convert.ToInt32(this._EditBox.Text.Trim()) - 1));
185 sr = st.Page(Convert.ToInt32(this._EditBox.Text.Trim()));
186 }
187 else
188 {
189 st = new SqlStatement(Gentle.Framework.StatementType.Select, cmd, ViewState["Sql"].ToString(), this._PageSize, this._PageSize * 0);
190 sr = st.Page(1);
191 }
192 this._DataSource = ObjectView.GetDataView(sr).Table;
193 //加载事件
194 if (btnClick != null)
195 btnClick(this, e);
196 }
197
198 #endregion
199
200 内部function#region 内部function
201
202 /**//// <summary>
203 /// 初始化数据源
204 /// </summary>
205 private void setPageload()
206 {
207
208 GentleSqlFactory sqlfact = Broker.GetSqlFactory();
209 IDbCommand cmd = sqlfact.GetCommand();
210
211 SqlStatement st = new SqlStatement(Gentle.Framework.StatementType.Select, cmd, ViewState["Sql"].ToString(), this._PageSize, this._PageSize * 0);
212 SqlResult sr = st.Page(1);
213 this._DataSource = ObjectView.GetDataView(sr).Table;
214 this._EditBox.Text = "1";
215 ViewState["PageCount"] = fn_getPages();
216 this._strCount.Text = ViewState["PageCount"].ToString();
217 }
218 /**//// <summary>
219 /// 取到总页数
220 /// </summary>
221 /// <returns></returns>
222 private string fn_getPages()
223 {
224 int rows = Broker.Execute(ViewState["Sql"].ToString()).Rows.Count;
225 string pagecount = Convert.ToString((rows / this.PageSize) + ((rows % this.PageSize) == 0 ? 0 : 1));
226 return pagecount;
227 }
228
229 #endregion
230
231 实现#region 实现
232
233 /**//// <summary>
234 /// 定义控件内容
235 /// </summary>
236 protected override void CreateChildControls()
237 {
238 try
239 {
240 this.Controls.Clear();
241
242 加载控件#region 加载控件
243 /**////转新页
244 this.Controls.Add(new LiteralControl("第"));
245 this._EditBox.ID = "EditBox";
246 this._EditBox.CssClass = "textbox";
247 this._EditBox.Columns = 2;
248 this._EditBox.EnableViewState = false;
249 this.Controls.Add(this._EditBox);
250 this.Controls.Add(new LiteralControl("页/共"));
251 /**////
252 if (ViewState["PageCount"] != null)
253 this._strCount.Text = ViewState["PageCount"].ToString();
254 this.Controls.Add(_strCount);
255 this.Controls.Add(new LiteralControl("页"));
256 //加入操作button
257 Button btnGo = new Button();
258 btnGo.ID = "btnGo";
259 btnGo.CssClass = "button";
260 btnGo.Text = "Go";
261 btnGo.OnClientClick = "javascript:if(fn_Check(document.all." + _EditBox.ClientID + ",'" + this._strCount.Text.Trim() + "','G')==false) return false;";
262 btnGo.Click += new EventHandler(btn_Click);
263 this.Controls.Add(btnGo);
264 this.Controls.Add(new LiteralControl(" "));
265 //第一页;
266 Button btnFirst = new Button();
267 btnFirst.ID = "btnFirst";
268 btnFirst.CssClass = "button";
269 btnFirst.Text = "第一页";
270 btnFirst.OnClientClick = "javascript:if(fn_Check(document.all." + _EditBox.ClientID + ",'" + this._strCount.Text.Trim() + "','F')==false) return false;";
271 btnFirst.Click += new EventHandler(First_Click);
272 this.Controls.Add(btnFirst);
273 this.Controls.Add(new LiteralControl(" "));
274 //上一页
275 Button btnPre = new Button();
276 btnPre.ID = "btnPre";
277 btnPre.Text = "上一页";
278 btnPre.CssClass = "button";
279 btnPre.OnClientClick = "javascript:if(fn_Check(document.all." + _EditBox.ClientID + ",'" + this._strCount.Text.Trim() + "','P')==false) return false;";
280 btnPre.Click += new EventHandler(Pre_Click);
281 this.Controls.Add(btnPre);
282 this.Controls.Add(new LiteralControl(" "));
283
284 /**////下一页
285 Button btnNext = new Button();
286 btnNext.ID = "btnNext";
287 btnNext.CssClass = "button";
288 btnNext.Text = "下一页";
289 btnNext.OnClientClick = "javascript:if(fn_Check(document.all." + _EditBox.ClientID + ",'" + this._strCount.Text.Trim() + "','N')==false) return false;";
290 btnNext.Click += new EventHandler(Next_Click);
291 this.Controls.Add(btnNext);
292 this.Controls.Add(new LiteralControl(" "));
293 /**////最后页
294 Button btnLast = new Button();
295 btnLast.ID = "btnLast";
296 btnLast.CssClass = "button";
297 btnLast.Text = "最后页";
298 btnLast.OnClientClick = "javascript:if(fn_Check(document.all." + _EditBox.ClientID + ",'" + this._strCount.Text.Trim() + "','L')==false) return false;";
299 btnLast.Click += new EventHandler(Last_Click);
300 this.Controls.Add(btnLast);
301 #endregion
302
303 }
304 catch (Exception exception2)
305 {
306 throw exception2;
307 }
308 }
309
310 /**//// <summary>
311 /// 实现接口function
312 /// </summary>
313 /// <param name="postDataKey"></param>
314 /// <param name="postCollection"></param>
315 /// <returns></returns>
316 public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
317 {
318 return false;
319 }
320
321 /**//// <summary>
322 /// 实现接口function
323 /// </summary>
324 public virtual void RaisePostDataChangedEvent()
325 {
326 try
327 {
328 //OnTextChanged(new Object(), new EventArgs());
329 }
330 catch (Exception exception2)
331 {
332 throw exception2;
333 }
334 }
335
336 /**//// <summary>
337 /// 重载funciton
338 /// </summary>
339 /// <param name="writer"></param>
340 protected override void Render(HtmlTextWriter writer)
341 {
342 this.EnsureChildControls();
343 base.Render(writer);
344
345 }
346
347 /**//// <summary>
348 /// 加入css及js
349 /// </summary>
350 /// <param name="e"></param>
351 protected override void OnPreRender(EventArgs e)
352 {
353 加入css#region 加入css
354 if (strCssLink == null || strCssLink == String.Empty)
355 {
356 string strcss = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), CSSResName);
357 strCssLink = string.Format("<link href='{0}' rel='stylesheet' type='text/css' title='green' />", strcss);
358 }
359 string cssKey = "ExtendDateNamecss";
360 if (this.Page.Header.FindControl(cssKey) != null)
361 return;
362 Literal ltlCss = new Literal();
363 ltlCss = new Literal();
364 ltlCss.ID = cssKey;
365 ltlCss.Text = strCssLink;
366
367 this.Parent.Page.Header.Controls.Add(ltlCss);
368 /**////
369 if (strJsLink == null || strJsLink == String.Empty)
370 {
371 string strjs = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), JSResName);
372 strJsLink = string.Format("<script src='{0}' type='text/javascript'></script>", strjs);
373 }
374 string jsKey = "ExtendDateNamejs";
375 if (this.Page.Header.FindControl(jsKey) != null)
376 return;
377 Literal ltljs = new Literal();
378 ltljs = new Literal();
379 ltljs.ID = jsKey;
380 ltljs.Text = strJsLink;
381 this.Parent.Page.Header.Controls.Add(ltljs);
382 #endregion
383 base.OnPreRender(e);
384 }
385
386 /**//// <summary>
387 /// 重载function
388 /// </summary>
389 public override void DataBind()
390 {
391 try
392 {
393 base.DataBind();
394 }
395 catch (Exception exception2)
396 {
397 throw exception2;
398 }
399 }
400
401 #endregion
402 }
403}
404
2、Calendar.js
///判断当前页
function fn_Check(currpage,allpages,flg){
currpage=parseInt(currpage.value.trim());
allpages=parseInt(allpages);
switch(flg){
case "F":
if(currpage==1){
alert("It is the first page");
return false;
}else{
return true;
}
break;
case "P":
if(currpage-1<=1){
alert("It is the first page");
return false;
}else{
return true;
}
break;
case "L":
if(currpage>=allpages){
alert("It is the last page");
return false;
}else{
return true;
}
break;
case "N":
if(currpage+1>=allpages){
alert("It is the last page");
return false;
}else{
return true;
}
break;
case "G":
if(currpage>allpages||currpage<1){
alert("No page");
return false;
}else{
return true;
}
break;
}
return true;
}
3、Calendar.css
.button
{}{
/**//*参考button样式--huiqin*/
FILTER: progid:DXImageTransform.Microsoft.Gradient(startColorStr='white', endColorStr='aliceblue', gradientType='0');
COLOR: #333333;
FONT-FAMILY: verdana;
FONT-SIZE: 9pt;
border: 1px solid aliceblue;
}
.textbox
{}{
background:#ffffff;
border-bottom-color:#CCCCCC;
border-bottom-width:1px;
border-top-width:1px;
border-left-width:1px;
border-right-width:1px;
/**//*color:Gray;*/
text-decoration:none;/**//*文本样式*/
word-spacing:inherit;/**//*字间距离*/
font-size: 10pt; font-style:normal;
font-family:"宋体,Tahoma";bottom:0px;
height: 15px;/**//*文本框高度*/
width:30px;/**//*文本框宽度*/
line-height: normal
}
4、测试,调用
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PagerCtl.Sql = " select * from dbo.m_Units ";
this.GridView1.DataSource = this.PagerCtl.DataSource;
this.GridView1.DataBind();
}
}
protected void PagerCtl_btnClick(object sender, EventArgs e)
{
this.GridView1.DataSource = this.PagerCtl.DataSource;
this.GridView1.DataBind();
}
-
相关阅读:
AJAX需要注意的
SEO 搜索引擎优化
jQuery 插件
CSS BFC和IE Haslayout
jQuery 插件开发
jQuery Ajax
jQuery 工具函数
jQuery 动画效果
jQuery 高级事件
jQuery 事件对象
-
原文地址:https://www.cnblogs.com/freeliver54/p/637995.html
Copyright © 2020-2023
润新知