NextPreviousPagerField.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. field.ButtonCssClass = ButtonCssClass;
  54. field.ButtonType = ButtonType;
  55. field.FirstPageImageUrl = FirstPageImageUrl;
  56. field.FirstPageText = FirstPageText;
  57. field.LastPageImageUrl = LastPageImageUrl;
  58. field.LastPageText = LastPageText;
  59. field.NextPageImageUrl = NextPageImageUrl;
  60. field.NextPageText = NextPageText;
  61. field.PreviousPageImageUrl = PreviousPageImageUrl;
  62. field.PreviousPageText = PreviousPageText;
  63. field.ShowFirstPageButton = ShowFirstPageButton;
  64. field.ShowLastPageButton = ShowLastPageButton;
  65. field.ShowNextPageButton = ShowNextPageButton;
  66. field.ShowPreviousPageButton = ShowPreviousPageButton;
  67. }
  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. break;
  134. case ButtonType.Link:
  135. LinkButton lbtn = new LinkButton ();
  136. lbtn.CommandName = commandName;
  137. lbtn.Text = text;
  138. ctl = lbtn;
  139. break;
  140. case ButtonType.Image:
  141. ImageButton ibtn = new ImageButton ();
  142. ibtn.CommandName = commandName;
  143. ibtn.ImageUrl = imageUrl;
  144. ibtn.AlternateText = text;
  145. ctl = ibtn;
  146. break;
  147. }
  148. if (ctl != null) {
  149. ctl.Enabled = enabled;
  150. ctl.CssClass = ButtonCssClass;
  151. }
  152. }
  153. }
  154. if (ctl != null) {
  155. container.Controls.Add (ctl);
  156. if (addNonBreakingSpace)
  157. container.Controls.Add (new LiteralControl ("&nbsp;"));
  158. }
  159. }
  160. protected override DataPagerField CreateField ()
  161. {
  162. return new NextPreviousPagerField ();
  163. }
  164. public override bool Equals (object o)
  165. {
  166. NextPreviousPagerField field = o as NextPreviousPagerField;
  167. if (field == null)
  168. return false;
  169. // Compare using the properties that are copied in CopyProperties
  170. if (field.ButtonCssClass != ButtonCssClass)
  171. return false;
  172. if (field.ButtonType != ButtonType)
  173. return false;
  174. if (field.FirstPageImageUrl != FirstPageImageUrl)
  175. return false;
  176. if (field.FirstPageText != FirstPageText)
  177. return false;
  178. if (field.LastPageImageUrl != LastPageImageUrl)
  179. return false;
  180. if (field.LastPageText != LastPageText)
  181. return false;
  182. if (field.NextPageImageUrl != NextPageImageUrl)
  183. return false;
  184. if (field.NextPageText != NextPageText)
  185. return false;
  186. if (field.PreviousPageImageUrl != PreviousPageImageUrl)
  187. return false;
  188. if (field.PreviousPageText != PreviousPageText)
  189. return false;
  190. if (field.ShowFirstPageButton != ShowFirstPageButton)
  191. return false;
  192. if (field.ShowLastPageButton != ShowLastPageButton)
  193. return false;
  194. if (field.ShowNextPageButton != ShowNextPageButton)
  195. return false;
  196. if (field.ShowPreviousPageButton != ShowPreviousPageButton)
  197. return false;
  198. return true;
  199. }
  200. public override int GetHashCode ()
  201. {
  202. int ret = 0;
  203. // Base the calculation on the properties that are copied in CopyProperties
  204. ret |= ButtonCssClass.GetHashCode ();
  205. ret |= ButtonType.GetHashCode ();
  206. ret |= FirstPageImageUrl.GetHashCode ();
  207. ret |= FirstPageText.GetHashCode ();
  208. ret |= LastPageImageUrl.GetHashCode ();
  209. ret |= LastPageText.GetHashCode ();
  210. ret |= NextPageImageUrl.GetHashCode ();
  211. ret |= NextPageText.GetHashCode ();
  212. ret |= PreviousPageImageUrl.GetHashCode ();
  213. ret |= PreviousPageText.GetHashCode ();
  214. ret |= ShowFirstPageButton.GetHashCode ();
  215. ret |= ShowLastPageButton.GetHashCode ();
  216. ret |= ShowNextPageButton.GetHashCode ();
  217. ret |= ShowPreviousPageButton.GetHashCode ();
  218. return ret;
  219. }
  220. public override void HandleEvent (CommandEventArgs e)
  221. {
  222. string commandName = e.CommandName;
  223. int newStartIndex = -1;
  224. int pageSize = DataPager.PageSize;
  225. if (String.Compare (commandName, DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0)
  226. newStartIndex = 0;
  227. else if (String.Compare (commandName, DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  228. int lastPageMod = _totalRowCount % pageSize;
  229. if (lastPageMod == 0)
  230. newStartIndex = _totalRowCount - pageSize;
  231. else
  232. newStartIndex = _totalRowCount - lastPageMod;
  233. } else if (String.Compare (commandName, DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  234. newStartIndex = _startRowIndex + pageSize;
  235. if (newStartIndex > _totalRowCount)
  236. newStartIndex = _totalRowCount - pageSize;
  237. } else if (String.Compare (commandName, DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  238. newStartIndex = _startRowIndex - pageSize;
  239. if (newStartIndex < 0)
  240. newStartIndex = 0;
  241. }
  242. if (newStartIndex != -1)
  243. DataPager.SetPageProperties (newStartIndex, pageSize, true);
  244. }
  245. public string ButtonCssClass {
  246. get {
  247. string s = ViewState ["ButtonCssClass"] as string;
  248. if (s != null)
  249. return s;
  250. return String.Empty;
  251. }
  252. set { ViewState ["ButtonCssClass"] = value; }
  253. }
  254. public ButtonType ButtonType {
  255. get {
  256. object o = ViewState ["ButtonType"];
  257. if (o != null)
  258. return (ButtonType) o;
  259. return ButtonType.Link;
  260. }
  261. set { ViewState ["ButtonType"] = value; }
  262. }
  263. public string FirstPageImageUrl {
  264. get {
  265. string s = ViewState ["FirstPageImageUrl"] as string;
  266. if (s != null)
  267. return s;
  268. return String.Empty;
  269. }
  270. set { ViewState ["FirstPageImageUrl"] = value; }
  271. }
  272. public string FirstPageText {
  273. get {
  274. string s = ViewState ["FirstPageText"] as string;
  275. if (s != null)
  276. return s;
  277. return "First";
  278. }
  279. set { ViewState ["FirstPageText"] = value; }
  280. }
  281. public string LastPageImageUrl {
  282. get {
  283. string s = ViewState ["LastPageImageUrl"] as string;
  284. if (s != null)
  285. return s;
  286. return String.Empty;
  287. }
  288. set { ViewState ["LastPageImageUrl"] = value; }
  289. }
  290. public string LastPageText {
  291. get {
  292. string s = ViewState ["LastPageText"] as string;
  293. if (s != null)
  294. return s;
  295. return "Last";
  296. }
  297. set { ViewState ["LastPageText"] = value; }
  298. }
  299. public string NextPageImageUrl {
  300. get {
  301. string s = ViewState ["NextPageImageUrl"] as string;
  302. if (s != null)
  303. return s;
  304. return String.Empty;
  305. }
  306. set { ViewState ["NextPageImageUrl"] = value; }
  307. }
  308. public string NextPageText {
  309. get {
  310. string s = ViewState ["NextPageText"] as string;
  311. if (s != null)
  312. return s;
  313. return "Next";
  314. }
  315. set { ViewState ["NextPageText"] = value; }
  316. }
  317. public string PreviousPageImageUrl {
  318. get {
  319. string s = ViewState ["PreviousPageImageUrl"] as string;
  320. if (s != null)
  321. return s;
  322. return String.Empty;
  323. }
  324. set { ViewState ["PreviousPageImageUrl"] = value; }
  325. }
  326. public string PreviousPageText {
  327. get {
  328. string s = ViewState ["PreviousPageText"] as string;
  329. if (s != null)
  330. return s;
  331. return "Previous";
  332. }
  333. set { ViewState ["PreviousPageText"] = value; }
  334. }
  335. public bool RenderDisabledButtonsAsLabels {
  336. get {
  337. object o = ViewState ["RenderDisabledButtonsAsLabels"];
  338. if (o != null)
  339. return (bool) o;
  340. return false;
  341. }
  342. set { ViewState ["RenderDisabledButtonsAsLabels"] = value; }
  343. }
  344. public bool RenderNonBreakingSpacesBetweenControls {
  345. get {
  346. object o = ViewState ["RenderNonBreakingSpacesBetweenControls"];
  347. if (o != null)
  348. return (bool) o;
  349. return true;
  350. }
  351. set { ViewState ["RenderNonBreakingSpacesBetweenControls"] = value; }
  352. }
  353. public bool ShowFirstPageButton {
  354. get {
  355. object o = ViewState ["ShowFirstPageButton"];
  356. if (o != null)
  357. return (bool) o;
  358. return false;
  359. }
  360. set { ViewState ["ShowFirstPageButton"] = value; }
  361. }
  362. public bool ShowLastPageButton {
  363. get {
  364. object o = ViewState ["ShowLastPageButton"];
  365. if (o != null)
  366. return (bool) o;
  367. return false;
  368. }
  369. set { ViewState ["ShowLastPageButton"] = value; }
  370. }
  371. public bool ShowNextPageButton {
  372. get {
  373. object o = ViewState ["ShowNextPageButton"];
  374. if (o != null)
  375. return (bool) o;
  376. return true;
  377. }
  378. set { ViewState ["ShowNextPageButton"] = value; }
  379. }
  380. public bool ShowPreviousPageButton {
  381. get {
  382. object o = ViewState ["ShowPreviousPageButton"];
  383. if (o != null)
  384. return (bool) o;
  385. return true;
  386. }
  387. set { ViewState ["ShowPreviousPageButton"] = value; }
  388. }
  389. }
  390. }
  391. #endif