想法:想让REPATER实现分页的功能并让已有的REPEATER控件比较复用。
做法: 继承OUT OF BOX的REPEATER,然后利用PAGEDDATASOURCE这种类型的数据源实现分页。最后定义了一个FOOTTEMPLATE类来实现ITEMPLATE接口实现分页的FOOTER CONTROL
PagedRepeater.cs //这个类主要是继承默认的Repeater 然后增加了一个PAGEDDATASOURCE 属性 |
1: [Designer("PagedRepeater")] 2: public class PagedRepeater: Repeater 3: {
4: public PagedDataSource pds 5: {
6: get;
7: set;
8: }
9: private int _pageSize; 10: public int PageSize 11: {
12: set
13: {
14: _pageSize = value; 15: }
16: get
17: {
18: if (pds != null) 19: {
20: return pds.PageSize; 21: }
22: else 23: {
24: return 0; 25: }
26: }
27: }
28: private int _currentPageIndex; 29: public int CurrentPageIndex 30: {
31:
32: get
33: {
34: if (ViewState["CurrentPageIndex"] != null) 35: {
36: _currentPageIndex = Int32.Parse(ViewState["CurrentPageIndex"].ToString()); 37: return _currentPageIndex; 38: }
39: else 40: {
41: return 0; 42: }
43: }
44: set
45: {
46: _currentPageIndex = value; 47: ViewState["CurrentPageIndex"] = _currentPageIndex; 48: }
49: }
50: public int PageCount 51: {
52: get {
53: if (pds != null) 54: {
55: return pds.PageCount; 56: }
57: else 58: {
59: return 0; 60: }
61: }
62: }
63: public override void DataBind() 64: {
65: if (HttpContext.Current.Request.Form["__EVENTTARGET"]!=null&&HttpContext.Current.Request.Form["__EVENTTARGET"].ToLower().Contains("pagedrp")) 66: {
67: string postbackdata = HttpContext.Current.Request.Form["__EVENTARGUMENT"]; 68: CurrentPageIndex = Int32.Parse(postbackdata);
69: }
70: pds.CurrentPageIndex = CurrentPageIndex;
71: pds.AllowPaging = true; 72: pds.PageSize = 10;
73: this.DataSource = pds; 74: this.FooterTemplate = new FooterTemplate(CurrentPageIndex, PageSize, PageCount); 75: base.DataBind(); 76:
77: }
78: protected override void OnInit(EventArgs e) 79: {
80: base.OnInit(e); 81: pds = new PagedDataSource(); 82: }
83: }
84:
|
FooterTemplate.cs // 这个实现ITEMPLATE 接口 |
1: public class FooterTemplate:ITemplate 2: {
3: public int PageSize 4: {
5: get;
6: set;
7: }
8: public int CurrentPageIndex 9: {
10: get;
11: set;
12: }
13:
14:
15: public int PageCount 16: {
17: get;
18: set;
19: }
20:
21: public FooterTemplate(int pageCurrentIndex, int pageSize, int pageCount) 22: {
23: this.PageSize = pageSize; 24: this.CurrentPageIndex = pageCurrentIndex; 25: this.PageCount = pageCount; 26: }
27:
28: public string LinkTemplate 29: {
30: get
31: {
32: return "<a οnclick='" + "javascript:__doPostBack(\"pagedrp\",{1})'" + ">{0}</a>"; 33: }
34:
35: }
36:
37: public virtual Control GenerateHyperLink() 38: {
39: var control = new LiteralControl(); 40: for (int i = 1; i <= this.PageCount; i++) 41: {
42: control.Text = control.Text + string.Format(LinkTemplate, i,i-1); 43: }
44: return control; 45: }
46: public void InstantiateIn(Control container) 47: {
48: container.Controls.Add(GenerateHyperLink());
49: }
50: }
|
调用 |
1: protected override void OnPreRender(EventArgs e) 2: {
3: base.OnPreRender(e); 4: List<YourClass> list = GetYourClassList();
5: repeater1.pds.DataSource = list;
6: repeater1.DataBind();
7: }
|
结果: |
这样一个简单的分页功能就能够实现了, 希望大家多多指教!