NextPreviousPagerField.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //
  2. // System.Web.UI.WebControls.NextPreviousPagerField
  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. #if NET_3_5
  30. using System;
  31. using System.ComponentModel;
  32. using System.Security.Permissions;
  33. using System.Web;
  34. using System.Web.UI;
  35. namespace System.Web.UI.WebControls
  36. {
  37. [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class NextPreviousPagerField : DataPagerField
  40. {
  41. int _startRowIndex;
  42. int _maximumRows;
  43. int _totalRowCount;
  44. int _fieldIndex;
  45. public NextPreviousPagerField ()
  46. {
  47. }
  48. protected override void CopyProperties (DataPagerField newField)
  49. {
  50. base.CopyProperties (newField);
  51. NextPreviousPagerField field = newField as NextPreviousPagerField;
  52. if (field == null)
  53. return;
  54. field.ButtonCssClass = ButtonCssClass;
  55. field.ButtonType = ButtonType;
  56. field.FirstPageImageUrl = FirstPageImageUrl;
  57. field.FirstPageText = FirstPageText;
  58. field.LastPageImageUrl = LastPageImageUrl;
  59. field.LastPageText = LastPageText;
  60. field.NextPageImageUrl = NextPageImageUrl;
  61. field.NextPageText = NextPageText;
  62. field.PreviousPageImageUrl = PreviousPageImageUrl;
  63. field.PreviousPageText = PreviousPageText;
  64. field.ShowFirstPageButton = ShowFirstPageButton;
  65. field.ShowLastPageButton = ShowLastPageButton;
  66. field.ShowNextPageButton = ShowNextPageButton;
  67. field.ShowPreviousPageButton = ShowPreviousPageButton;
  68. }
  69. // What's the fieldIndex used for?
  70. public override void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows, int totalRowCount, int fieldIndex)
  71. {
  72. _startRowIndex = startRowIndex;
  73. _maximumRows = maximumRows;
  74. _totalRowCount = totalRowCount;
  75. _fieldIndex = fieldIndex;
  76. bool setPagePropertiesNeeded = false;
  77. bool queryMode = GetQueryModeStartRowIndex (_totalRowCount, _maximumRows, ref _startRowIndex, ref setPagePropertiesNeeded);
  78. bool enablePrevFirst = _startRowIndex >= _maximumRows;
  79. bool enableNextLast = (_startRowIndex + _maximumRows) < _totalRowCount;
  80. bool addNonBreakingSpace = RenderNonBreakingSpacesBetweenControls;
  81. if (ShowFirstPageButton)
  82. CreateButton (container, DataControlCommands.FirstPageCommandArgument, FirstPageText, FirstPageImageUrl, 0,
  83. queryMode, enablePrevFirst, addNonBreakingSpace);
  84. int newPageNum = -1;
  85. if (ShowPreviousPageButton) {
  86. if (queryMode)
  87. newPageNum = (_startRowIndex / _maximumRows) - 1;
  88. CreateButton (container, DataControlCommands.PreviousPageCommandArgument, PreviousPageText, PreviousPageImageUrl, newPageNum,
  89. queryMode, enablePrevFirst, addNonBreakingSpace);
  90. }
  91. if (ShowNextPageButton) {
  92. if (queryMode)
  93. newPageNum = (_startRowIndex + _maximumRows) / _maximumRows;
  94. CreateButton (container, DataControlCommands.NextPageCommandArgument, NextPageText, NextPageImageUrl, newPageNum,
  95. queryMode, enableNextLast, addNonBreakingSpace);
  96. }
  97. if (ShowLastPageButton) {
  98. if (queryMode) {
  99. newPageNum = _totalRowCount / _maximumRows;
  100. if ((_totalRowCount % _maximumRows) == 0)
  101. newPageNum--;
  102. }
  103. CreateButton (container, DataControlCommands.LastPageCommandArgument, LastPageText, LastPageImageUrl, newPageNum,
  104. queryMode, enableNextLast, addNonBreakingSpace);
  105. }
  106. if (setPagePropertiesNeeded)
  107. DataPager.SetPageProperties (_startRowIndex, _maximumRows, true);
  108. }
  109. void CreateButton (DataPagerFieldItem container, string commandName, string text, string imageUrl, int pageNum,
  110. bool queryMode, bool enabled, bool addNonBreakingSpace)
  111. {
  112. WebControl ctl = null;
  113. if (queryMode) {
  114. pageNum++;
  115. HyperLink h = new HyperLink ();
  116. h.Text = text;
  117. h.ImageUrl = imageUrl;
  118. h.Enabled = enabled;
  119. h.NavigateUrl = GetQueryStringNavigateUrl (pageNum);
  120. h.CssClass = ButtonCssClass;
  121. ctl = h;
  122. } else {
  123. if (!enabled && RenderDisabledButtonsAsLabels) {
  124. Label l = new Label ();
  125. l.Text = text;
  126. ctl = l;
  127. } else {
  128. switch (ButtonType) {
  129. case ButtonType.Button:
  130. Button btn = new Button ();
  131. btn.CommandName = commandName;
  132. btn.Text = text;
  133. ctl = btn;
  134. break;
  135. case ButtonType.Link:
  136. LinkButton lbtn = new LinkButton ();
  137. lbtn.CommandName = commandName;
  138. lbtn.Text = text;
  139. ctl = lbtn;
  140. break;
  141. case ButtonType.Image:
  142. ImageButton ibtn = new ImageButton ();
  143. ibtn.CommandName = commandName;
  144. ibtn.ImageUrl = imageUrl;
  145. ibtn.AlternateText = text;
  146. ctl = ibtn;
  147. break;
  148. }
  149. if (ctl != null) {
  150. ctl.Enabled = enabled;
  151. ctl.CssClass = ButtonCssClass;
  152. }
  153. }
  154. }
  155. if (ctl != null) {
  156. container.Controls.Add (ctl);
  157. if (addNonBreakingSpace)
  158. container.Controls.Add (new LiteralControl ("&nbsp;"));
  159. }
  160. }
  161. protected override DataPagerField CreateField ()
  162. {
  163. return new NextPreviousPagerField ();
  164. }
  165. public override bool Equals (object o)
  166. {
  167. NextPreviousPagerField field = o as NextPreviousPagerField;
  168. if (field == null)
  169. return false;
  170. // Compare using the properties that are copied in CopyProperties
  171. if (field.ButtonCssClass != ButtonCssClass)
  172. return false;
  173. if (field.ButtonType != ButtonType)
  174. return false;
  175. if (field.FirstPageImageUrl != FirstPageImageUrl)
  176. return false;
  177. if (field.FirstPageText != FirstPageText)
  178. return false;
  179. if (field.LastPageImageUrl != LastPageImageUrl)
  180. return false;
  181. if (field.LastPageText != LastPageText)
  182. return false;
  183. if (field.NextPageImageUrl != NextPageImageUrl)
  184. return false;
  185. if (field.NextPageText != NextPageText)
  186. return false;
  187. if (field.PreviousPageImageUrl != PreviousPageImageUrl)
  188. return false;
  189. if (field.PreviousPageText != PreviousPageText)
  190. return false;
  191. if (field.ShowFirstPageButton != ShowFirstPageButton)
  192. return false;
  193. if (field.ShowLastPageButton != ShowLastPageButton)
  194. return false;
  195. if (field.ShowNextPageButton != ShowNextPageButton)
  196. return false;
  197. if (field.ShowPreviousPageButton != ShowPreviousPageButton)
  198. return false;
  199. return true;
  200. }
  201. public override int GetHashCode ()
  202. {
  203. int ret = 0;
  204. // Base the calculation on the properties that are copied in CopyProperties
  205. ret |= ButtonCssClass.GetHashCode ();
  206. ret |= ButtonType.GetHashCode ();
  207. ret |= FirstPageImageUrl.GetHashCode ();
  208. ret |= FirstPageText.GetHashCode ();
  209. ret |= LastPageImageUrl.GetHashCode ();
  210. ret |= LastPageText.GetHashCode ();
  211. ret |= NextPageImageUrl.GetHashCode ();
  212. ret |= NextPageText.GetHashCode ();
  213. ret |= PreviousPageImageUrl.GetHashCode ();
  214. ret |= PreviousPageText.GetHashCode ();
  215. ret |= ShowFirstPageButton.GetHashCode ();
  216. ret |= ShowLastPageButton.GetHashCode ();
  217. ret |= ShowNextPageButton.GetHashCode ();
  218. ret |= ShowPreviousPageButton.GetHashCode ();
  219. return ret;
  220. }
  221. public override void HandleEvent (CommandEventArgs e)
  222. {
  223. string commandName = e.CommandName;
  224. int newStartIndex = -1;
  225. int pageSize = DataPager.PageSize;
  226. if (String.Compare (commandName, DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0)
  227. newStartIndex = 0;
  228. else if (String.Compare (commandName, DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  229. int lastPageMod = _totalRowCount % pageSize;
  230. if (lastPageMod == 0)
  231. newStartIndex = _totalRowCount - pageSize;
  232. else
  233. newStartIndex = _totalRowCount - lastPageMod;
  234. } else if (String.Compare (commandName, DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  235. newStartIndex = _startRowIndex + pageSize;
  236. if (_totalRowCount >= 0 && newStartIndex > _totalRowCount)
  237. newStartIndex = _totalRowCount - pageSize;
  238. } else if (String.Compare (commandName, DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  239. newStartIndex = _startRowIndex - pageSize;
  240. if (newStartIndex < 0)
  241. newStartIndex = 0;
  242. }
  243. if (newStartIndex >= 0)
  244. DataPager.SetPageProperties (newStartIndex, pageSize, true);
  245. }
  246. public string ButtonCssClass {
  247. get {
  248. string s = ViewState ["ButtonCssClass"] as string;
  249. if (s != null)
  250. return s;
  251. return String.Empty;
  252. }
  253. set { ViewState ["ButtonCssClass"] = value; }
  254. }
  255. public ButtonType ButtonType {
  256. get {
  257. object o = ViewState ["ButtonType"];
  258. if (o != null)
  259. return (ButtonType) o;
  260. return ButtonType.Link;
  261. }
  262. set { ViewState ["ButtonType"] = value; }
  263. }
  264. public string FirstPageImageUrl {
  265. get {
  266. string s = ViewState ["FirstPageImageUrl"] as string;
  267. if (s != null)
  268. return s;
  269. return String.Empty;
  270. }
  271. set { ViewState ["FirstPageImageUrl"] = value; }
  272. }
  273. public string FirstPageText {
  274. get {
  275. string s = ViewState ["FirstPageText"] as string;
  276. if (s != null)
  277. return s;
  278. return "First";
  279. }
  280. set { ViewState ["FirstPageText"] = value; }
  281. }
  282. public string LastPageImageUrl {
  283. get {
  284. string s = ViewState ["LastPageImageUrl"] as string;
  285. if (s != null)
  286. return s;
  287. return String.Empty;
  288. }
  289. set { ViewState ["LastPageImageUrl"] = value; }
  290. }
  291. public string LastPageText {
  292. get {
  293. string s = ViewState ["LastPageText"] as string;
  294. if (s != null)
  295. return s;
  296. return "Last";
  297. }
  298. set { ViewState ["LastPageText"] = value; }
  299. }
  300. public string NextPageImageUrl {
  301. get {
  302. string s = ViewState ["NextPageImageUrl"] as string;
  303. if (s != null)
  304. return s;
  305. return String.Empty;
  306. }
  307. set { ViewState ["NextPageImageUrl"] = value; }
  308. }
  309. public string NextPageText {
  310. get {
  311. string s = ViewState ["NextPageText"] as string;
  312. if (s != null)
  313. return s;
  314. return "Next";
  315. }
  316. set { ViewState ["NextPageText"] = value; }
  317. }
  318. public string PreviousPageImageUrl {
  319. get {
  320. string s = ViewState ["PreviousPageImageUrl"] as string;
  321. if (s != null)
  322. return s;
  323. return String.Empty;
  324. }
  325. set { ViewState ["PreviousPageImageUrl"] = value; }
  326. }
  327. public string PreviousPageText {
  328. get {
  329. string s = ViewState ["PreviousPageText"] as string;
  330. if (s != null)
  331. return s;
  332. return "Previous";
  333. }
  334. set { ViewState ["PreviousPageText"] = value; }
  335. }
  336. public bool RenderDisabledButtonsAsLabels {
  337. get {
  338. object o = ViewState ["RenderDisabledButtonsAsLabels"];
  339. if (o != null)
  340. return (bool) o;
  341. return false;
  342. }
  343. set { ViewState ["RenderDisabledButtonsAsLabels"] = value; }
  344. }
  345. public bool RenderNonBreakingSpacesBetweenControls {
  346. get {
  347. object o = ViewState ["RenderNonBreakingSpacesBetweenControls"];
  348. if (o != null)
  349. return (bool) o;
  350. return true;
  351. }
  352. set { ViewState ["RenderNonBreakingSpacesBetweenControls"] = value; }
  353. }
  354. public bool ShowFirstPageButton {
  355. get {
  356. object o = ViewState ["ShowFirstPageButton"];
  357. if (o != null)
  358. return (bool) o;
  359. return false;
  360. }
  361. set { ViewState ["ShowFirstPageButton"] = value; }
  362. }
  363. public bool ShowLastPageButton {
  364. get {
  365. object o = ViewState ["ShowLastPageButton"];
  366. if (o != null)
  367. return (bool) o;
  368. return false;
  369. }
  370. set { ViewState ["ShowLastPageButton"] = value; }
  371. }
  372. public bool ShowNextPageButton {
  373. get {
  374. object o = ViewState ["ShowNextPageButton"];
  375. if (o != null)
  376. return (bool) o;
  377. return true;
  378. }
  379. set { ViewState ["ShowNextPageButton"] = value; }
  380. }
  381. public bool ShowPreviousPageButton {
  382. get {
  383. object o = ViewState ["ShowPreviousPageButton"];
  384. if (o != null)
  385. return (bool) o;
  386. return true;
  387. }
  388. set { ViewState ["ShowPreviousPageButton"] = value; }
  389. }
  390. }
  391. }
  392. #endif