PagedControl.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Project : Mono
  23. * Namespace : System.Web.UI.MobileControls
  24. * Class : PagedControl
  25. * Author : Gaurav Vaish
  26. *
  27. * Copyright : 2003 with Gaurav Vaish, and with
  28. * Ximian Inc
  29. */
  30. using System.Web.UI;
  31. using System.Web.Mobile;
  32. namespace System.Web.UI.MobileControls
  33. {
  34. public abstract class PagedControl : MobileControl
  35. {
  36. private static readonly object LoadItemsEvent = new object();
  37. private int itemCount = 0;
  38. private ItemPager itemPager;
  39. private bool pagingCharsChanged = false;
  40. protected PagedControl()
  41. {
  42. }
  43. public event LoadItemsEventHandler LoadItems
  44. {
  45. add
  46. {
  47. Events.AddHandler(LoadItemsEvent, value);
  48. }
  49. remove
  50. {
  51. Events.RemoveHandler(LoadItemsEvent, value);
  52. }
  53. }
  54. private int PagerItemIndex
  55. {
  56. get
  57. {
  58. return (itemPager == null ? 0 : itemPager.ItemIndex);
  59. }
  60. }
  61. private int PagerItemCount
  62. {
  63. get
  64. {
  65. return (itemPager == null ? InternalItemCount :
  66. itemPager.ItemCount);
  67. }
  68. }
  69. protected abstract int InternalItemCount { get; }
  70. public int FirstVisibleItemIndex
  71. {
  72. get
  73. {
  74. if(!IsCustomPaging && EnablePagination)
  75. return PagerItemIndex;
  76. return 0;
  77. }
  78. }
  79. private bool IsCustomPaging
  80. {
  81. get
  82. {
  83. return (itemCount > 0);
  84. }
  85. }
  86. public int ItemCount
  87. {
  88. get
  89. {
  90. return itemCount;
  91. }
  92. set
  93. {
  94. itemCount = value;
  95. }
  96. }
  97. public int ItemWeight
  98. {
  99. get
  100. {
  101. return ControlPager.DefaultWeight;
  102. }
  103. }
  104. public int ItemsPerPage
  105. {
  106. get
  107. {
  108. object o = ViewState["ItemsPerPage"];
  109. if(o != null)
  110. return (int)o;
  111. return 0;
  112. }
  113. set
  114. {
  115. ViewState["ItemsPerPage"] = value;
  116. }
  117. }
  118. public int VisibleItemCount
  119. {
  120. get
  121. {
  122. if(IsCustomPaging || !EnablePagination)
  123. return InternalItemCount;
  124. return PagerItemCount;
  125. }
  126. }
  127. public override int VisibleWeight
  128. {
  129. get
  130. {
  131. if(VisibleItemCount == -1)
  132. return 0;
  133. return VisibleItemCount * GetItemWeight();
  134. }
  135. }
  136. private int GetItemWeight()
  137. {
  138. int iw = Adapter.ItemWeight;
  139. if(iw == ControlPager.UseDefaultWeight)
  140. return ItemWeight;
  141. return iw;
  142. }
  143. protected virtual void OnLoadItems(LoadItemsEventArgs e)
  144. {
  145. LoadItemsEventHandler lieh = (LoadItemsEventHandler)(Events[LoadItemsEvent]);
  146. if(lieh != null)
  147. lieh(this, e);
  148. }
  149. private void OnLoadItems()
  150. {
  151. OnLoadItems(new LoadItemsEventArgs(PagerItemIndex, PagerItemCount));
  152. }
  153. protected override void OnPageChange(int oldPageIndex, int newPageIndex)
  154. {
  155. pagingCharsChanged = true;
  156. }
  157. protected override void OnPreRender(EventArgs e)
  158. {
  159. if(IsCustomPaging)
  160. {
  161. if(!Page.IsPostBack || Form.PaginationStateChanged
  162. || pagingCharsChanged || !IsViewStateEnabled())
  163. {
  164. OnLoadItems();
  165. }
  166. }
  167. base.OnPreRender(e);
  168. }
  169. private bool IsViewStateEnabled()
  170. {
  171. Control ctrl = this;
  172. while(ctrl != null && ctrl.EnableViewState)
  173. {
  174. ctrl = ctrl.Parent;
  175. }
  176. return (ctrl == null);
  177. }
  178. public override void PaginateRecursive(ControlPager pager)
  179. {
  180. int ic = 0;
  181. int ipp = 0;
  182. if(IsCustomPaging || InternalItemCount == 0)
  183. ic = ItemCount;
  184. ipp = ItemsPerPage;
  185. itemPager = pager.GetItemPager(this, ic, ipp, GetItemWeight());
  186. }
  187. }
  188. }