PagerSettings.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // System.Web.UI.WebControls.PagerSettings.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  29. //
  30. #if NET_2_0
  31. using System.ComponentModel;
  32. using System.Security.Permissions;
  33. namespace System.Web.UI.WebControls
  34. {
  35. [TypeConverterAttribute (typeof(ExpandableObjectConverter))]
  36. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public sealed class PagerSettings: IStateManager
  38. {
  39. StateBag ViewState = new StateBag ();
  40. Control ctrl;
  41. public PagerSettings ()
  42. {
  43. }
  44. internal PagerSettings (Control ctrl)
  45. {
  46. this.ctrl = ctrl;
  47. }
  48. [WebCategoryAttribute ("Appearance")]
  49. [NotifyParentPropertyAttribute (true)]
  50. [UrlPropertyAttribute]
  51. [DefaultValueAttribute ("")]
  52. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  53. public string FirstPageImageUrl {
  54. get {
  55. object ob = ViewState ["FirstPageImageUrl"];
  56. if (ob != null) return (string) ob;
  57. return string.Empty;
  58. }
  59. set {
  60. ViewState ["FirstPageImageUrl"] = value;
  61. RaisePropertyChanged ();
  62. }
  63. }
  64. [WebCategoryAttribute ("Appearance")]
  65. [DefaultValueAttribute ("<<")]
  66. [NotifyParentPropertyAttribute (true)]
  67. public string FirstPageText {
  68. get {
  69. object ob = ViewState ["FirstPageText"];
  70. if (ob != null) return (string) ob;
  71. return "<<";
  72. }
  73. set {
  74. ViewState ["FirstPageText"] = value;
  75. RaisePropertyChanged ();
  76. }
  77. }
  78. [WebCategoryAttribute ("Appearance")]
  79. [NotifyParentPropertyAttribute (true)]
  80. [UrlPropertyAttribute]
  81. [DefaultValueAttribute ("")]
  82. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  83. public string LastPageImageUrl {
  84. get {
  85. object ob = ViewState ["LastPageImageUrl"];
  86. if (ob != null) return (string) ob;
  87. return string.Empty;
  88. }
  89. set {
  90. ViewState ["LastPageImageUrl"] = value;
  91. RaisePropertyChanged ();
  92. }
  93. }
  94. [NotifyParentPropertyAttribute (true)]
  95. [WebCategoryAttribute ("Appearance")]
  96. [DefaultValueAttribute (">>")]
  97. public string LastPageText {
  98. get {
  99. object ob = ViewState ["LastPageText"];
  100. if (ob != null) return (string) ob;
  101. return ">>";
  102. }
  103. set {
  104. ViewState ["LastPageText"] = value;
  105. RaisePropertyChanged ();
  106. }
  107. }
  108. [NotifyParentPropertyAttribute (true)]
  109. [WebCategoryAttribute ("Appearance")]
  110. [DefaultValueAttribute (PagerButtons.Numeric)]
  111. public PagerButtons Mode {
  112. get {
  113. object ob = ViewState ["Mode"];
  114. if (ob != null) return (PagerButtons) ob;
  115. return PagerButtons.Numeric;
  116. }
  117. set {
  118. ViewState ["Mode"] = value;
  119. RaisePropertyChanged ();
  120. }
  121. }
  122. [WebCategoryAttribute ("Appearance")]
  123. [NotifyParentPropertyAttribute (true)]
  124. [UrlPropertyAttribute]
  125. [DefaultValueAttribute ("")]
  126. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  127. public string NextPageImageUrl {
  128. get {
  129. object ob = ViewState ["NextPageImageUrl"];
  130. if (ob != null) return (string) ob;
  131. return string.Empty;
  132. }
  133. set {
  134. ViewState ["NextPageImageUrl"] = value;
  135. RaisePropertyChanged ();
  136. }
  137. }
  138. [WebCategoryAttribute ("Appearance")]
  139. [NotifyParentPropertyAttribute (true)]
  140. [DefaultValueAttribute (">")]
  141. public string NextPageText {
  142. get {
  143. object ob = ViewState ["NextPageText"];
  144. if (ob != null) return (string) ob;
  145. return ">";
  146. }
  147. set {
  148. ViewState ["NextPageText"] = value;
  149. RaisePropertyChanged ();
  150. }
  151. }
  152. [WebCategoryAttribute ("Behavior")]
  153. [NotifyParentPropertyAttribute (true)]
  154. [DefaultValueAttribute (10)]
  155. public int PageButtonCount {
  156. get {
  157. object ob = ViewState ["PageButtonCount"];
  158. if (ob != null) return (int) ob;
  159. return 10;
  160. }
  161. set {
  162. ViewState ["PageButtonCount"] = value;
  163. RaisePropertyChanged ();
  164. }
  165. }
  166. [WebCategoryAttribute ("Layout")]
  167. [DefaultValueAttribute (PagerPosition.Bottom)]
  168. [NotifyParentPropertyAttribute (true)]
  169. public PagerPosition Position {
  170. get {
  171. object ob = ViewState ["Position"];
  172. if (ob != null) return (PagerPosition) ob;
  173. return PagerPosition.Bottom;
  174. }
  175. set {
  176. ViewState ["Position"] = value;
  177. }
  178. }
  179. [WebCategoryAttribute ("Appearance")]
  180. [NotifyParentPropertyAttribute (true)]
  181. [UrlPropertyAttribute]
  182. [DefaultValueAttribute ("")]
  183. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  184. public string PreviousPageImageUrl {
  185. get {
  186. object ob = ViewState ["PreviousPageImageUrl"];
  187. if (ob != null) return (string) ob;
  188. return string.Empty;
  189. }
  190. set {
  191. ViewState ["PreviousPageImageUrl"] = value;
  192. RaisePropertyChanged ();
  193. }
  194. }
  195. [WebCategoryAttribute ("Appearance")]
  196. [DefaultValueAttribute ("<")]
  197. [NotifyParentPropertyAttribute (true)]
  198. public string PreviousPageText {
  199. get {
  200. object ob = ViewState ["PreviousPageText"];
  201. if (ob != null) return (string) ob;
  202. return "<";
  203. }
  204. set {
  205. ViewState ["PreviousPageText"] = value;
  206. RaisePropertyChanged ();
  207. }
  208. }
  209. [WebCategoryAttribute ("Appearance")]
  210. [DefaultValueAttribute (true)]
  211. [NotifyParentPropertyAttribute (true)]
  212. public bool Visible {
  213. get {
  214. object ob = ViewState ["Visible"];
  215. if (ob != null) return (bool) ob;
  216. return true;
  217. }
  218. set {
  219. ViewState ["Visible"] = value;
  220. }
  221. }
  222. public event EventHandler PropertyChanged;
  223. void RaisePropertyChanged ()
  224. {
  225. if (PropertyChanged != null)
  226. PropertyChanged (this, EventArgs.Empty);
  227. }
  228. public override string ToString ()
  229. {
  230. return string.Empty;
  231. }
  232. void IStateManager.LoadViewState (object savedState)
  233. {
  234. ViewState.LoadViewState (savedState);
  235. }
  236. object IStateManager.SaveViewState ()
  237. {
  238. return ViewState.SaveViewState();
  239. }
  240. void IStateManager.TrackViewState ()
  241. {
  242. ViewState.TrackViewState();
  243. }
  244. bool IStateManager.IsTrackingViewState
  245. {
  246. get { return ViewState.IsTrackingViewState; }
  247. }
  248. internal Table CreatePagerControl (int currentPage, int pageCount, Style style)
  249. {
  250. Table table = new Table ();
  251. TableRow row = new TableRow ();
  252. table.Rows.Add (row);
  253. if (Mode == PagerButtons.NextPrevious || Mode == PagerButtons.NextPreviousFirstLast)
  254. {
  255. if (currentPage > 0) {
  256. if (Mode == PagerButtons.NextPreviousFirstLast)
  257. row.Cells.Add (CreateCell (FirstPageText, FirstPageImageUrl, "Page", "First", style));
  258. row.Cells.Add (CreateCell (PreviousPageText, PreviousPageImageUrl, "Page", "Prev", style));
  259. }
  260. if (currentPage < pageCount - 1) {
  261. row.Cells.Add (CreateCell (NextPageText, NextPageImageUrl, "Page", "Next", style));
  262. if (Mode == PagerButtons.NextPreviousFirstLast)
  263. row.Cells.Add (CreateCell (LastPageText, LastPageImageUrl, "Page", "Last", style));
  264. }
  265. }
  266. else if (Mode == PagerButtons.Numeric || Mode == PagerButtons.NumericFirstLast)
  267. {
  268. int pbc = PageButtonCount;
  269. int cp = currentPage + 1;
  270. int pbp = pbc <= cp ? cp / pbc : 0;
  271. int first = cp < pbc ? 0 : (cp + (pbp % pbc) - (pbc + pbp)) + 1;
  272. int last = first + pbc;
  273. if (last >= pageCount) last = pageCount;
  274. if (first > 0) {
  275. if (Mode == PagerButtons.NumericFirstLast)
  276. row.Cells.Add (CreateCell (FirstPageText, FirstPageImageUrl, "Page", "First", style));
  277. row.Cells.Add (CreateCell (PreviousPageText, PreviousPageImageUrl, "Page", "Prev", style));
  278. }
  279. for (int n = first; n < last; n++)
  280. row.Cells.Add (CreateCell ((n + 1).ToString (), string.Empty, (n != currentPage) ? "Page" : "", (n + 1).ToString (), style));
  281. if (last < pageCount - 1) {
  282. row.Cells.Add (CreateCell (NextPageText, NextPageImageUrl, "Page", "Next", style));
  283. if (Mode == PagerButtons.NumericFirstLast)
  284. row.Cells.Add (CreateCell (LastPageText, LastPageImageUrl, "Page", "Last", style));
  285. }
  286. }
  287. return table;
  288. }
  289. TableCell CreateCell (string text, string image, string command, string argument, Style style) {
  290. TableCell cell = new TableCell ();
  291. DataControlButton button = new DataControlButton (ctrl, text, image, command, argument, true);
  292. if (image.Length > 0)
  293. button.ButtonType = ButtonType.Image;
  294. else if (command.Length > 0 && style != null)
  295. button.ForeColor = style.ForeColor;
  296. cell.Controls.Add (button);
  297. return cell;
  298. }
  299. }
  300. }
  301. #endif