Mercurial > pub > bltoolkit
comparison Demo/Asp.Net/Web/App_Code/CustomList.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 CustomList : DataList | |
| 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 | |
| 79 query = RX.Replace(query, string.Empty); | |
| 80 | |
| 81 // Write out the first part of the control, the table header | |
| 82 // | |
| 83 writer.Write("<table cellpadding=0 cellspacing=0><tr><td colspan=2>"); | |
| 84 | |
| 85 // Call the inherited method | |
| 86 // | |
| 87 base.Render(writer); | |
| 88 | |
| 89 // Write out a table row closure | |
| 90 // | |
| 91 writer.Write("</td></tr><tr><td class=paging align=left>"); | |
| 92 | |
| 93 // Determin whether next and previous buttons are required Previous button? | |
| 94 // | |
| 95 if (_currentPageIndex > 0) | |
| 96 writer.Write(string.Format("<a href=?page={0}>< Previous</a>", _currentPageIndex - 1 + query)); | |
| 97 | |
| 98 // Close the table data tag | |
| 99 // | |
| 100 writer.Write("</td><td align=right class=paging>"); | |
| 101 | |
| 102 // Next button? | |
| 103 // | |
| 104 if (_currentPageIndex < PageCount) | |
| 105 writer.Write(string.Format("<a href=?page={0}>More ></a>", _currentPageIndex + 1 + query)); | |
| 106 | |
| 107 // Close the table | |
| 108 // | |
| 109 writer.Write("</td></tr></table>"); | |
| 110 } | |
| 111 | |
| 112 protected override void OnDataBinding(EventArgs e) | |
| 113 { | |
| 114 // Work out which items we want to render to the page | |
| 115 // | |
| 116 int start = CurrentPageIndex * _pageSize; | |
| 117 int size = Math.Min(_pageSize, ItemCount - start); | |
| 118 | |
| 119 IList page = new ArrayList(); | |
| 120 | |
| 121 // Add the relevant items from the datasource | |
| 122 // | |
| 123 for (int i = 0; i < size; i++) | |
| 124 page.Add(_dataSource[start + i]); | |
| 125 | |
| 126 // set the base objects datasource | |
| 127 // | |
| 128 base.DataSource = page; | |
| 129 base.OnDataBinding(e); | |
| 130 } | |
| 131 | |
| 132 public event DataGridPageChangedEventHandler PageIndexChanged; | |
| 133 | |
| 134 virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e) | |
| 135 { | |
| 136 if (PageIndexChanged != null) | |
| 137 PageIndexChanged(this, e); | |
| 138 } | |
| 139 } | |
| 140 } |
