PagerSettings.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. static readonly object propertyChangedEvent = new object ();
  40. StateBag ViewState = new StateBag ();
  41. Control ctrl;
  42. EventHandlerList events = new EventHandlerList ();
  43. [Browsable (false)]
  44. public event EventHandler PropertyChanged {
  45. add { events.AddHandler (propertyChangedEvent, value); }
  46. remove { events.RemoveHandler (propertyChangedEvent, value); }
  47. }
  48. public PagerSettings ()
  49. {
  50. }
  51. internal PagerSettings (Control ctrl)
  52. {
  53. this.ctrl = ctrl;
  54. }
  55. [WebCategoryAttribute ("Appearance")]
  56. [NotifyParentPropertyAttribute (true)]
  57. [UrlPropertyAttribute]
  58. [DefaultValueAttribute ("")]
  59. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  60. public string FirstPageImageUrl {
  61. get {
  62. object ob = ViewState ["FirstPageImageUrl"];
  63. if (ob != null) return (string) ob;
  64. return string.Empty;
  65. }
  66. set {
  67. ViewState ["FirstPageImageUrl"] = value;
  68. RaisePropertyChanged ();
  69. }
  70. }
  71. [WebCategoryAttribute ("Appearance")]
  72. [DefaultValueAttribute ("<<")]
  73. [NotifyParentPropertyAttribute (true)]
  74. public string FirstPageText {
  75. get {
  76. object ob = ViewState ["FirstPageText"];
  77. if (ob != null) return (string) ob;
  78. return "<<";
  79. }
  80. set {
  81. ViewState ["FirstPageText"] = value;
  82. RaisePropertyChanged ();
  83. }
  84. }
  85. [WebCategoryAttribute ("Appearance")]
  86. [NotifyParentPropertyAttribute (true)]
  87. [UrlPropertyAttribute]
  88. [DefaultValueAttribute ("")]
  89. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  90. public string LastPageImageUrl {
  91. get {
  92. object ob = ViewState ["LastPageImageUrl"];
  93. if (ob != null) return (string) ob;
  94. return string.Empty;
  95. }
  96. set {
  97. ViewState ["LastPageImageUrl"] = value;
  98. RaisePropertyChanged ();
  99. }
  100. }
  101. [NotifyParentPropertyAttribute (true)]
  102. [WebCategoryAttribute ("Appearance")]
  103. [DefaultValueAttribute (">>")]
  104. public string LastPageText {
  105. get {
  106. object ob = ViewState ["LastPageText"];
  107. if (ob != null) return (string) ob;
  108. return ">>";
  109. }
  110. set {
  111. ViewState ["LastPageText"] = value;
  112. RaisePropertyChanged ();
  113. }
  114. }
  115. [NotifyParentPropertyAttribute (true)]
  116. [WebCategoryAttribute ("Appearance")]
  117. [DefaultValueAttribute (PagerButtons.Numeric)]
  118. public PagerButtons Mode {
  119. get {
  120. object ob = ViewState ["Mode"];
  121. if (ob != null) return (PagerButtons) ob;
  122. return PagerButtons.Numeric;
  123. }
  124. set {
  125. ViewState ["Mode"] = value;
  126. RaisePropertyChanged ();
  127. }
  128. }
  129. [WebCategoryAttribute ("Appearance")]
  130. [NotifyParentPropertyAttribute (true)]
  131. [UrlPropertyAttribute]
  132. [DefaultValueAttribute ("")]
  133. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  134. public string NextPageImageUrl {
  135. get {
  136. object ob = ViewState ["NextPageImageUrl"];
  137. if (ob != null) return (string) ob;
  138. return string.Empty;
  139. }
  140. set {
  141. ViewState ["NextPageImageUrl"] = value;
  142. RaisePropertyChanged ();
  143. }
  144. }
  145. [WebCategoryAttribute ("Appearance")]
  146. [NotifyParentPropertyAttribute (true)]
  147. [DefaultValueAttribute (">")]
  148. public string NextPageText {
  149. get {
  150. object ob = ViewState ["NextPageText"];
  151. if (ob != null) return (string) ob;
  152. return ">";
  153. }
  154. set {
  155. ViewState ["NextPageText"] = value;
  156. RaisePropertyChanged ();
  157. }
  158. }
  159. [WebCategoryAttribute ("Behavior")]
  160. [NotifyParentPropertyAttribute (true)]
  161. [DefaultValueAttribute (10)]
  162. public int PageButtonCount {
  163. get {
  164. object ob = ViewState ["PageButtonCount"];
  165. if (ob != null) return (int) ob;
  166. return 10;
  167. }
  168. set {
  169. ViewState ["PageButtonCount"] = value;
  170. RaisePropertyChanged ();
  171. }
  172. }
  173. [WebCategoryAttribute ("Layout")]
  174. [DefaultValueAttribute (PagerPosition.Bottom)]
  175. [NotifyParentPropertyAttribute (true)]
  176. public PagerPosition Position {
  177. get {
  178. object ob = ViewState ["Position"];
  179. if (ob != null) return (PagerPosition) ob;
  180. return PagerPosition.Bottom;
  181. }
  182. set {
  183. ViewState ["Position"] = value;
  184. }
  185. }
  186. [WebCategoryAttribute ("Appearance")]
  187. [NotifyParentPropertyAttribute (true)]
  188. [UrlPropertyAttribute]
  189. [DefaultValueAttribute ("")]
  190. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  191. public string PreviousPageImageUrl {
  192. get {
  193. object ob = ViewState ["PreviousPageImageUrl"];
  194. if (ob != null) return (string) ob;
  195. return string.Empty;
  196. }
  197. set {
  198. ViewState ["PreviousPageImageUrl"] = value;
  199. RaisePropertyChanged ();
  200. }
  201. }
  202. [WebCategoryAttribute ("Appearance")]
  203. [DefaultValueAttribute ("<")]
  204. [NotifyParentPropertyAttribute (true)]
  205. public string PreviousPageText {
  206. get {
  207. object ob = ViewState ["PreviousPageText"];
  208. if (ob != null) return (string) ob;
  209. return "<";
  210. }
  211. set {
  212. ViewState ["PreviousPageText"] = value;
  213. RaisePropertyChanged ();
  214. }
  215. }
  216. [WebCategoryAttribute ("Appearance")]
  217. [DefaultValueAttribute (true)]
  218. [NotifyParentPropertyAttribute (true)]
  219. public bool Visible {
  220. get {
  221. object ob = ViewState ["Visible"];
  222. if (ob != null) return (bool) ob;
  223. return true;
  224. }
  225. set {
  226. ViewState ["Visible"] = value;
  227. }
  228. }
  229. void RaisePropertyChanged ()
  230. {
  231. EventHandler eh = events [propertyChangedEvent] as EventHandler;
  232. if (eh != null)
  233. eh (this, EventArgs.Empty);
  234. }
  235. public override string ToString ()
  236. {
  237. return string.Empty;
  238. }
  239. void IStateManager.LoadViewState (object savedState)
  240. {
  241. ViewState.LoadViewState (savedState);
  242. }
  243. object IStateManager.SaveViewState ()
  244. {
  245. return ViewState.SaveViewState();
  246. }
  247. void IStateManager.TrackViewState ()
  248. {
  249. ViewState.TrackViewState();
  250. }
  251. bool IStateManager.IsTrackingViewState
  252. {
  253. get { return ViewState.IsTrackingViewState; }
  254. }
  255. internal Table CreatePagerControl (int currentPage, int pageCount)
  256. {
  257. Table table = new Table ();
  258. TableRow row = new TableRow ();
  259. table.Rows.Add (row);
  260. int buttonCount = (Mode == PagerButtons.Numeric || Mode == PagerButtons.NumericFirstLast) ? PageButtonCount : 1;
  261. int first = buttonCount * (currentPage / buttonCount);
  262. int last = first + buttonCount;
  263. if (last > pageCount) {
  264. last = pageCount;
  265. if (last - first < buttonCount)
  266. first = last - buttonCount;
  267. if (first < 0)
  268. first = 0;
  269. }
  270. // First button
  271. if (Mode == PagerButtons.NumericFirstLast || Mode == PagerButtons.NextPreviousFirstLast) {
  272. if (first > 0)
  273. row.Cells.Add (CreateCell (FirstPageText, FirstPageImageUrl, "Page", "First"));
  274. }
  275. // Prev button
  276. if (Mode == PagerButtons.NextPrevious || Mode == PagerButtons.NextPreviousFirstLast) {
  277. if (first > 0)
  278. row.Cells.Add (CreateCell (PreviousPageText, PreviousPageImageUrl, "Page", "Prev"));
  279. }
  280. // Numbers
  281. if (Mode == PagerButtons.Numeric || Mode == PagerButtons.NumericFirstLast) {
  282. if (first > 0)
  283. row.Cells.Add (CreateCell ("...", string.Empty, "Page", first.ToString ()));
  284. for (int n = first; n < last; n++)
  285. row.Cells.Add (CreateCell ((n + 1).ToString (), string.Empty, (n != currentPage) ? "Page" : "", (n != currentPage) ? (n + 1).ToString () : ""));
  286. if (last < pageCount)
  287. row.Cells.Add (CreateCell ("...", string.Empty, "Page", (last + 1).ToString ()));
  288. }
  289. // Next button
  290. if (Mode == PagerButtons.NextPrevious || Mode == PagerButtons.NextPreviousFirstLast) {
  291. if (last < pageCount)
  292. row.Cells.Add (CreateCell (NextPageText, NextPageImageUrl, "Page", "Next"));
  293. }
  294. // Last button
  295. if (Mode == PagerButtons.NumericFirstLast || Mode == PagerButtons.NextPreviousFirstLast) {
  296. if (last < pageCount)
  297. row.Cells.Add (CreateCell (LastPageText, LastPageImageUrl, "Page", "Last"));
  298. }
  299. return table;
  300. }
  301. TableCell CreateCell (string text, string image, string command, string argument) {
  302. TableCell cell = new TableCell ();
  303. Control button;
  304. if (String.IsNullOrEmpty (command)) {
  305. Label l = new Label ();
  306. l.Text = text;
  307. button = l;
  308. }
  309. else
  310. button = (Control)DataControlButton.CreateButton (String.IsNullOrEmpty (image) ? ButtonType.Link : ButtonType.Image, ctrl, text, image, command, argument, true);
  311. cell.Controls.Add (button);
  312. return cell;
  313. }
  314. }
  315. }
  316. #endif