comparison Demo/Asp.Net/Web/App_Code/CustomGrid.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.Text.RegularExpressions;
4 using System.Web.UI;
5 using System.Web.UI.WebControls;
6
7 namespace PetShop.Web
8 {
9 public class CustomGrid : Repeater
10 {
11 private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
12
13 protected string _emptyText;
14 private IList _dataSource;
15 private int _pageSize = 10;
16 private int _currentPageIndex;
17 private int _itemCount;
18
19 public override object DataSource
20 {
21 set
22 {
23 // This try catch block is to avoid issues with the VS.NET designer
24 // The designer will try and bind a datasource which does not derive from ILIST
25 try
26 {
27 _dataSource = (IList)value;
28 ItemCount = _dataSource.Count;
29 }
30 catch
31 {
32 _dataSource = null;
33 ItemCount = 0;
34 }
35 }
36 }
37
38 public int PageSize { get { return _pageSize; } set { _pageSize = value; } }
39 protected virtual int ItemCount { get { return _itemCount; } set { _itemCount = value; } }
40 public virtual int CurrentPageIndex { get { return _currentPageIndex; } set { _currentPageIndex = value; } }
41
42 protected int PageCount { get { return (ItemCount - 1) / _pageSize; } }
43 public string EmptyText { set { _emptyText = value; } }
44
45 public void SetPage(int index)
46 {
47 OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
48 }
49
50 protected override void OnLoad(EventArgs e)
51 {
52 if (Visible)
53 {
54 string page = Context.Request["page"];
55 int index = page != null ? int.Parse(page) : 0;
56
57 SetPage(index);
58 }
59 }
60
61 /// <summary>
62 /// Overridden method to control how the page is rendered
63 /// </summary>
64 /// <param name="writer"></param>
65 protected override void Render(HtmlTextWriter writer)
66 {
67 // Check there is some data attached
68 //
69 if (ItemCount == 0)
70 {
71 writer.Write(_emptyText);
72 return;
73 }
74
75 // Mask the query
76 //
77 string query = Context.Request.Url.Query.Replace("?", "&");
78 query = RX.Replace(query, string.Empty);
79
80 // Write out the first part of the control, the table header
81 //
82 writer.Write("<table cellpadding=0 cellspacing=0><tr><td colspan=2>");
83
84 // Call the inherited method
85 //
86 base.Render(writer);
87
88 // Write out a table row closure
89 //
90 writer.Write("</td></tr><tr><td class=paging align=left>");
91
92 // Determin whether next and previous buttons are required Previous button?
93 //
94 if (_currentPageIndex > 0)
95 writer.Write(string.Format("<a href=?page={0}>&#060;&nbsp;Previous</a>", _currentPageIndex - 1 + query));
96
97 // Close the table data tag
98 //
99 writer.Write("</td><td align=right class=paging>");
100
101 // Next button?
102 //
103 if (_currentPageIndex < PageCount)
104 writer.Write(string.Format("<a href=?page={0}>More&nbsp;&#062;</a>", _currentPageIndex + 1 + query));
105
106 // Close the table
107 //
108 writer.Write("</td></tr></table>");
109 }
110
111 protected override void OnDataBinding(EventArgs e)
112 {
113 // Work out which items we want to render to the page
114 //
115 int start = CurrentPageIndex * _pageSize;
116 int size = Math.Min(_pageSize, ItemCount - start);
117
118 IList page = new ArrayList();
119
120 // Add the relevant items from the datasource
121 //
122 for (int i = 0; i < size; i++)
123 page.Add(_dataSource[start + i]);
124
125 // Set the base objects datasource
126 //
127 base.DataSource = page;
128 base.OnDataBinding(e);
129 }
130
131 public event DataGridPageChangedEventHandler PageIndexChanged;
132
133 protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e)
134 {
135 if (PageIndexChanged != null)
136 PageIndexChanged(this, e);
137 }
138 }
139 }