DataPagerField.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.Web.UI.WebControls.DataPagerField
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007 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. #if NET_3_5
  30. using System;
  31. using System.Collections.Specialized;
  32. using System.ComponentModel;
  33. using System.Globalization;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.Web;
  37. using System.Web.UI;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. public abstract class DataPagerField : IStateManager
  43. {
  44. StateBag _state = new StateBag ();
  45. DataPager _dataPager;
  46. bool _queryStringHandled;
  47. bool _isTrackingViewState;
  48. string _queryStringNavigateUrl;
  49. protected DataPagerField ()
  50. {
  51. }
  52. protected internal DataPagerField CloneField ()
  53. {
  54. DataPagerField ret = CreateField ();
  55. CopyProperties (ret);
  56. return ret;
  57. }
  58. protected virtual void CopyProperties (DataPagerField newField)
  59. {
  60. // assuming we should copy only the public properties
  61. newField.Visible = Visible;
  62. }
  63. public abstract void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows,
  64. int totalRowCount, int fieldIndex);
  65. protected abstract DataPagerField CreateField ();
  66. protected string GetQueryStringNavigateUrl (int pageNumber)
  67. {
  68. if (_queryStringNavigateUrl == null && _dataPager != null) {
  69. HttpContext ctx = HttpContext.Current;
  70. HttpRequest req = ctx != null ? ctx.Request : null;
  71. string queryFieldName = _dataPager.QueryStringField;
  72. if (req != null) {
  73. StringBuilder sb = new StringBuilder (req.Path + "?");
  74. NameValueCollection coll = req.QueryString;
  75. foreach (string k in coll.AllKeys) {
  76. if (String.Compare (k, queryFieldName, StringComparison.OrdinalIgnoreCase) == 0)
  77. continue;
  78. sb.Append (HttpUtility.UrlEncode (k) + "=" + HttpUtility.UrlEncode (coll [k]) + "&");
  79. }
  80. sb.Append (queryFieldName + "=");
  81. _queryStringNavigateUrl = sb.ToString ();
  82. } else
  83. _queryStringNavigateUrl = String.Empty;
  84. }
  85. return _queryStringNavigateUrl + pageNumber.ToString (CultureInfo.InvariantCulture);
  86. }
  87. public abstract void HandleEvent (CommandEventArgs e);
  88. protected virtual void LoadViewState (Object savedState)
  89. {
  90. if (savedState == null)
  91. return;
  92. ((IStateManager) ViewState).LoadViewState (savedState);
  93. }
  94. protected virtual void OnFieldChanged ()
  95. {
  96. if (FieldChanged != null)
  97. FieldChanged (this, EventArgs.Empty);
  98. }
  99. protected virtual object SaveViewState ()
  100. {
  101. return ((IStateManager) ViewState).SaveViewState ();
  102. }
  103. protected virtual void TrackViewState ()
  104. {
  105. _isTrackingViewState = true;
  106. ((IStateManager)ViewState).TrackViewState ();
  107. }
  108. protected DataPager DataPager {
  109. get { return _dataPager; }
  110. }
  111. protected bool QueryStringHandled {
  112. get { return _queryStringHandled; }
  113. set { _queryStringHandled = value; }
  114. }
  115. protected string QueryStringValue {
  116. get {
  117. if (_dataPager == null)
  118. return String.Empty;
  119. HttpContext ctx = HttpContext.Current;
  120. HttpRequest req = ctx != null ? ctx.Request : null;
  121. if (req == null)
  122. return String.Empty;
  123. return req.QueryString [_dataPager.QueryStringField];
  124. }
  125. }
  126. protected StateBag ViewState {
  127. get { return _state; }
  128. }
  129. public bool Visible {
  130. get {
  131. object o = ViewState ["Visible"];
  132. if (o == null)
  133. return true;
  134. return (bool) o;
  135. }
  136. set {
  137. if (value != Visible) {
  138. ViewState ["Visible"] = value;
  139. OnFieldChanged ();
  140. }
  141. }
  142. }
  143. void IStateManager.TrackViewState ()
  144. {
  145. TrackViewState ();
  146. }
  147. bool IStateManager.IsTrackingViewState {
  148. get { return _isTrackingViewState; }
  149. }
  150. object IStateManager.SaveViewState ()
  151. {
  152. return SaveViewState ();
  153. }
  154. void IStateManager.LoadViewState (object state)
  155. {
  156. LoadViewState (state);
  157. }
  158. internal event EventHandler FieldChanged;
  159. }
  160. }
  161. #endif