DataPagerField.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // System.Web.UI.WebControls.DataPagerField
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007-2008 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Specialized;
  31. using System.ComponentModel;
  32. using System.Globalization;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. using System.Web;
  36. using System.Web.UI;
  37. namespace System.Web.UI.WebControls
  38. {
  39. [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. public abstract class DataPagerField : IStateManager
  42. {
  43. static readonly object FieldChangedEvent = new object ();
  44. EventHandlerList events;
  45. StateBag _state = new StateBag ();
  46. DataPager _dataPager;
  47. bool _queryStringHandled;
  48. bool _isTrackingViewState;
  49. string _queryStringNavigateUrl;
  50. internal event EventHandler FieldChanged {
  51. add { AddEventHandler (FieldChangedEvent, value); }
  52. remove { RemoveEventHandler (FieldChangedEvent, value); }
  53. }
  54. protected DataPagerField ()
  55. {
  56. }
  57. protected internal DataPagerField CloneField ()
  58. {
  59. DataPagerField ret = CreateField ();
  60. CopyProperties (ret);
  61. return ret;
  62. }
  63. protected virtual void CopyProperties (DataPagerField newField)
  64. {
  65. // assuming we should copy only the public properties
  66. newField.Visible = Visible;
  67. }
  68. public abstract void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows,
  69. int totalRowCount, int fieldIndex);
  70. protected abstract DataPagerField CreateField ();
  71. protected string GetQueryStringNavigateUrl (int pageNumber)
  72. {
  73. if (_queryStringNavigateUrl == null && _dataPager != null) {
  74. HttpContext ctx = HttpContext.Current;
  75. HttpRequest req = ctx != null ? ctx.Request : null;
  76. string queryFieldName = _dataPager.QueryStringField;
  77. if (req != null) {
  78. StringBuilder sb = new StringBuilder (req.Path + "?");
  79. NameValueCollection coll = req.QueryString;
  80. foreach (string k in coll.AllKeys) {
  81. if (String.Compare (k, queryFieldName, StringComparison.OrdinalIgnoreCase) == 0)
  82. continue;
  83. sb.Append (HttpUtility.UrlEncode (k) + "=" + HttpUtility.UrlEncode (coll [k]) + "&");
  84. }
  85. sb.Append (queryFieldName + "=");
  86. _queryStringNavigateUrl = sb.ToString ();
  87. } else
  88. _queryStringNavigateUrl = String.Empty;
  89. }
  90. return _queryStringNavigateUrl + pageNumber.ToString (CultureInfo.InvariantCulture);
  91. }
  92. public abstract void HandleEvent (CommandEventArgs e);
  93. protected virtual void LoadViewState (Object savedState)
  94. {
  95. if (savedState == null)
  96. return;
  97. ((IStateManager) ViewState).LoadViewState (savedState);
  98. }
  99. protected virtual void OnFieldChanged ()
  100. {
  101. InvokeEvent (FieldChangedEvent, EventArgs.Empty);
  102. }
  103. protected virtual object SaveViewState ()
  104. {
  105. return ((IStateManager) ViewState).SaveViewState ();
  106. }
  107. protected virtual void TrackViewState ()
  108. {
  109. _isTrackingViewState = true;
  110. ((IStateManager)ViewState).TrackViewState ();
  111. }
  112. protected DataPager DataPager {
  113. get { return _dataPager; }
  114. }
  115. protected bool QueryStringHandled {
  116. get { return _queryStringHandled; }
  117. set { _queryStringHandled = value; }
  118. }
  119. protected string QueryStringValue {
  120. get {
  121. if (_dataPager == null)
  122. return String.Empty;
  123. HttpContext ctx = HttpContext.Current;
  124. HttpRequest req = ctx != null ? ctx.Request : null;
  125. if (req == null)
  126. return String.Empty;
  127. return req.QueryString [_dataPager.QueryStringField];
  128. }
  129. }
  130. protected StateBag ViewState {
  131. get { return _state; }
  132. }
  133. public bool Visible {
  134. get {
  135. object o = ViewState ["Visible"];
  136. if (o == null)
  137. return true;
  138. return (bool) o;
  139. }
  140. set {
  141. if (value != Visible) {
  142. ViewState ["Visible"] = value;
  143. OnFieldChanged ();
  144. }
  145. }
  146. }
  147. protected bool IsTrackingViewState {
  148. get { return _isTrackingViewState; }
  149. }
  150. void IStateManager.TrackViewState ()
  151. {
  152. TrackViewState ();
  153. }
  154. bool IStateManager.IsTrackingViewState {
  155. get { return IsTrackingViewState; }
  156. }
  157. object IStateManager.SaveViewState ()
  158. {
  159. return SaveViewState ();
  160. }
  161. void IStateManager.LoadViewState (object state)
  162. {
  163. LoadViewState (state);
  164. }
  165. internal void SetDataPager (DataPager pager)
  166. {
  167. _dataPager = pager;
  168. }
  169. internal bool GetQueryModeStartRowIndex (int totalRowCount, int maximumRows, ref int startRowIndex, ref bool setPagePropertiesNeeded)
  170. {
  171. bool queryMode = !String.IsNullOrEmpty (DataPager.QueryStringField);
  172. if (!queryMode || QueryStringHandled)
  173. return queryMode;
  174. QueryStringHandled = true;
  175. // We need to calculate the new start index since it is probably out
  176. // of date because the GET parameter with the page number hasn't
  177. // been processed yet
  178. int pageNumber;
  179. try {
  180. pageNumber = Int32.Parse (QueryStringValue);
  181. } catch {
  182. // ignore
  183. pageNumber = -1;
  184. }
  185. if (pageNumber >= 0) {
  186. pageNumber--; // we're zero-based since we're calculating
  187. // the offset/index
  188. if (pageNumber >= 0) {
  189. // zero-based calculation again
  190. int pageCount = (totalRowCount - 1) / maximumRows;
  191. if (pageNumber <= pageCount) {
  192. startRowIndex = pageNumber * maximumRows;
  193. setPagePropertiesNeeded = true;
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. void AddEventHandler (object key, EventHandler handler)
  200. {
  201. if (events == null)
  202. events = new EventHandlerList ();
  203. events.AddHandler (key, handler);
  204. }
  205. void RemoveEventHandler (object key, EventHandler handler)
  206. {
  207. if (events == null)
  208. return;
  209. events.RemoveHandler (key, handler);
  210. }
  211. void InvokeEvent (object key, EventArgs args)
  212. {
  213. if (events == null)
  214. return;
  215. EventHandler eh = events [key] as EventHandler;
  216. if (eh == null)
  217. return;
  218. eh (this, args);
  219. }
  220. }
  221. }