NextPreviousPagerField.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //
  2. // System.Web.UI.WebControls.NextPreviousPagerField
  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.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 queryMode = !String.IsNullOrEmpty (DataPager.QueryStringField);
  77. bool setPagePropertiesNeeded = false;
  78. if (queryMode && !QueryStringHandled) {
  79. QueryStringHandled = true;
  80. // We need to calculate the new start index since it is probably out
  81. // of date because the GET parameter with the page number hasn't
  82. // been processed yet
  83. int pageNumber = -1;
  84. try {
  85. pageNumber = Int32.Parse (QueryStringValue);
  86. } catch {
  87. // ignore
  88. }
  89. if (pageNumber >= 0) {
  90. pageNumber--; // we're zero-based since we're calculating
  91. // the offset/index
  92. if (pageNumber >= 0) {
  93. // zero-based calculation again
  94. int pageCount = (_totalRowCount - 1) / _maximumRows;
  95. if (pageNumber <= pageCount) {
  96. _startRowIndex = pageNumber * _maximumRows;
  97. setPagePropertiesNeeded = true;
  98. }
  99. }
  100. }
  101. }
  102. bool enablePrevFirst = _startRowIndex > _maximumRows;
  103. bool enableNextLast = (_startRowIndex + _maximumRows) < _totalRowCount;
  104. bool addNonBreakingSpace = RenderNonBreakingSpacesBetweenControls;
  105. if (ShowFirstPageButton)
  106. CreateButton (container, DataControlCommands.FirstPageCommandArgument, FirstPageText, FirstPageImageUrl, 0,
  107. queryMode, enablePrevFirst, addNonBreakingSpace);
  108. int newPageNum = -1;
  109. if (ShowNextPageButton) {
  110. if (queryMode)
  111. newPageNum = (_startRowIndex + _maximumRows) / _maximumRows;
  112. CreateButton (container, DataControlCommands.NextPageCommandArgument, NextPageText, NextPageImageUrl, newPageNum,
  113. queryMode, enableNextLast, addNonBreakingSpace);
  114. }
  115. if (ShowLastPageButton) {
  116. if (queryMode) {
  117. newPageNum = _totalRowCount / _maximumRows;
  118. if ((_totalRowCount % _maximumRows) == 0)
  119. newPageNum--;
  120. }
  121. CreateButton (container, DataControlCommands.LastPageCommandArgument, LastPageText, LastPageImageUrl, newPageNum,
  122. queryMode, enableNextLast, addNonBreakingSpace);
  123. }
  124. if (ShowPreviousPageButton) {
  125. if (queryMode)
  126. newPageNum = (_startRowIndex / _maximumRows) - 1;
  127. CreateButton (container, DataControlCommands.PreviousPageCommandArgument, PreviousPageText, PreviousPageImageUrl, newPageNum,
  128. queryMode, enablePrevFirst, addNonBreakingSpace);
  129. }
  130. if (setPagePropertiesNeeded)
  131. DataPager.SetPageProperties (_startRowIndex, _maximumRows, true);
  132. }
  133. void CreateButton (DataPagerFieldItem container, string commandName, string text, string imageUrl, int pageNum, bool queryMode,
  134. bool enabled, bool addNonBreakingSpace)
  135. {
  136. WebControl ctl = null;
  137. if (queryMode) {
  138. pageNum++;
  139. HyperLink h = new HyperLink ();
  140. h.Text = text;
  141. h.ImageUrl = imageUrl;
  142. h.Enabled = enabled;
  143. h.NavigateUrl = GetQueryStringNavigateUrl (pageNum);
  144. h.CssClass = ButtonCssClass;
  145. ctl = h;
  146. } else {
  147. if (!enabled && RenderDisabledButtonsAsLabels) {
  148. Label l = new Label ();
  149. l.Text = text;
  150. ctl = l;
  151. } else {
  152. switch (ButtonType) {
  153. case ButtonType.Button:
  154. ctl = new Button ();
  155. break;
  156. case ButtonType.Link:
  157. ctl = new LinkButton ();
  158. break;
  159. case ButtonType.Image:
  160. ImageButton btn = new ImageButton ();
  161. btn.ImageUrl = imageUrl;
  162. btn.AlternateText = text;
  163. ctl = btn;
  164. break;
  165. }
  166. if (ctl != null) {
  167. ctl.Enabled = enabled;
  168. ((Button)ctl).CommandName = commandName;
  169. ctl.CssClass = ButtonCssClass;
  170. }
  171. }
  172. }
  173. if (ctl != null) {
  174. container.Controls.Add (ctl);
  175. if (addNonBreakingSpace)
  176. container.Controls.Add (new LiteralControl ("&nbsp;"));
  177. }
  178. }
  179. protected override DataPagerField CreateField ()
  180. {
  181. return new NextPreviousPagerField ();
  182. }
  183. public override bool Equals (object o)
  184. {
  185. NextPreviousPagerField field = o as NextPreviousPagerField;
  186. if (field == null)
  187. return false;
  188. // Compare using the properties that are copied in CopyProperties
  189. if (field.ButtonCssClass != ButtonCssClass)
  190. return false;
  191. if (field.ButtonType != ButtonType)
  192. return false;
  193. if (field.FirstPageImageUrl != FirstPageImageUrl)
  194. return false;
  195. if (field.FirstPageText != FirstPageText)
  196. return false;
  197. if (field.LastPageImageUrl != LastPageImageUrl)
  198. return false;
  199. if (field.LastPageText != LastPageText)
  200. return false;
  201. if (field.NextPageImageUrl != NextPageImageUrl)
  202. return false;
  203. if (field.NextPageText != NextPageText)
  204. return false;
  205. if (field.PreviousPageImageUrl != PreviousPageImageUrl)
  206. return false;
  207. if (field.PreviousPageText != PreviousPageText)
  208. return false;
  209. if (field.ShowFirstPageButton != ShowFirstPageButton)
  210. return false;
  211. if (field.ShowLastPageButton != ShowLastPageButton)
  212. return false;
  213. if (field.ShowNextPageButton != ShowNextPageButton)
  214. return false;
  215. if (field.ShowPreviousPageButton != ShowPreviousPageButton)
  216. return false;
  217. return true;
  218. }
  219. public override int GetHashCode ()
  220. {
  221. int ret = 0;
  222. // Base the calculation on the properties that are copied in CopyProperties
  223. ret |= ButtonCssClass.GetHashCode ();
  224. ret |= ButtonType.GetHashCode ();
  225. ret |= FirstPageImageUrl.GetHashCode ();
  226. ret |= FirstPageText.GetHashCode ();
  227. ret |= LastPageImageUrl.GetHashCode ();
  228. ret |= LastPageText.GetHashCode ();
  229. ret |= NextPageImageUrl.GetHashCode ();
  230. ret |= NextPageText.GetHashCode ();
  231. ret |= PreviousPageImageUrl.GetHashCode ();
  232. ret |= PreviousPageText.GetHashCode ();
  233. ret |= ShowFirstPageButton.GetHashCode ();
  234. ret |= ShowLastPageButton.GetHashCode ();
  235. ret |= ShowNextPageButton.GetHashCode ();
  236. ret |= ShowPreviousPageButton.GetHashCode ();
  237. return ret;
  238. }
  239. public override void HandleEvent (CommandEventArgs e)
  240. {
  241. string commandName = e.CommandName;
  242. int newStartIndex = -1;
  243. int pageSize = DataPager.PageSize;
  244. if (String.Compare (commandName, DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  245. newStartIndex = 0;
  246. } else if (String.Compare (commandName, DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  247. int lastPageMod = _totalRowCount % pageSize;
  248. if (lastPageMod == 0)
  249. newStartIndex = _totalRowCount - pageSize;
  250. else
  251. newStartIndex = _totalRowCount - lastPageMod;
  252. } else if (String.Compare (commandName, DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  253. newStartIndex = _startRowIndex + pageSize;
  254. if (newStartIndex > _totalRowCount)
  255. newStartIndex = _totalRowCount - pageSize;
  256. } else if (String.Compare (commandName, DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
  257. newStartIndex = _startRowIndex - pageSize;
  258. if (newStartIndex < 0)
  259. newStartIndex = 0;
  260. }
  261. if (newStartIndex != -1)
  262. DataPager.SetPageProperties (newStartIndex, pageSize, true);
  263. }
  264. public string ButtonCssClass {
  265. get {
  266. string s = ViewState ["ButtonCssClass"] as string;
  267. if (s != null)
  268. return s;
  269. return String.Empty;
  270. }
  271. set { ViewState ["ButtonCssClass"] = value; }
  272. }
  273. public ButtonType ButtonType {
  274. get {
  275. object o = ViewState ["ButtonType"];
  276. if (o != null)
  277. return (ButtonType) o;
  278. return ButtonType.Link;
  279. }
  280. set { ViewState ["ButtonType"] = value; }
  281. }
  282. public string FirstPageImageUrl {
  283. get {
  284. string s = ViewState ["FirstPageImageUrl"] as string;
  285. if (s != null)
  286. return s;
  287. return String.Empty;
  288. }
  289. set { ViewState ["FirstPageImageUrl"] = value; }
  290. }
  291. public string FirstPageText {
  292. get {
  293. string s = ViewState ["FirstPageText"] as string;
  294. if (s != null)
  295. return s;
  296. return "First";
  297. }
  298. set { ViewState ["FirstPageText"] = value; }
  299. }
  300. public string LastPageImageUrl {
  301. get {
  302. string s = ViewState ["LastPageImageUrl"] as string;
  303. if (s != null)
  304. return s;
  305. return String.Empty;
  306. }
  307. set { ViewState ["LastPageImageUrl"] = value; }
  308. }
  309. public string LastPageText {
  310. get {
  311. string s = ViewState ["LastPageText"] as string;
  312. if (s != null)
  313. return s;
  314. return "Last";
  315. }
  316. set { ViewState ["LastPageText"] = value; }
  317. }
  318. public string NextPageImageUrl {
  319. get {
  320. string s = ViewState ["NextPageImageUrl"] as string;
  321. if (s != null)
  322. return s;
  323. return String.Empty;
  324. }
  325. set { ViewState ["NextPageImageUrl"] = value; }
  326. }
  327. public string NextPageText {
  328. get {
  329. string s = ViewState ["NextPageText"] as string;
  330. if (s != null)
  331. return s;
  332. return "Next";
  333. }
  334. set { ViewState ["NextPageText"] = value; }
  335. }
  336. public string PreviousPageImageUrl {
  337. get {
  338. string s = ViewState ["PreviousPageImageUrl"] as string;
  339. if (s != null)
  340. return s;
  341. return String.Empty;
  342. }
  343. set { ViewState ["PreviousPageImageUrl"] = value; }
  344. }
  345. public string PreviousPageText {
  346. get {
  347. string s = ViewState ["PreviousPageText"] as string;
  348. if (s != null)
  349. return s;
  350. return "Previous";
  351. }
  352. set { ViewState ["PreviousPageText"] = value; }
  353. }
  354. public bool RenderDisabledButtonsAsLabels {
  355. get {
  356. object o = ViewState ["RenderDisabledButtonsAsLabels"];
  357. if (o != null)
  358. return (bool) o;
  359. return false;
  360. }
  361. set { ViewState ["RenderDisabledButtonsAsLabels"] = value; }
  362. }
  363. public bool RenderNonBreakingSpacesBetweenControls {
  364. get {
  365. object o = ViewState ["RenderNonBreakingSpacesBetweenControls"];
  366. if (o != null)
  367. return (bool) o;
  368. return true;
  369. }
  370. set { ViewState ["RenderNonBreakingSpacesBetweenControls"] = value; }
  371. }
  372. public bool ShowFirstPageButton {
  373. get {
  374. object o = ViewState ["ShowFirstPageButton"];
  375. if (o != null)
  376. return (bool) o;
  377. return false;
  378. }
  379. set { ViewState ["ShowFirstPageButton"] = value; }
  380. }
  381. public bool ShowLastPageButton {
  382. get {
  383. object o = ViewState ["ShowLastPageButton"];
  384. if (o != null)
  385. return (bool) o;
  386. return false;
  387. }
  388. set { ViewState ["ShowLastPageButton"] = value; }
  389. }
  390. public bool ShowNextPageButton {
  391. get {
  392. object o = ViewState ["ShowNextPageButton"];
  393. if (o != null)
  394. return (bool) o;
  395. return true;
  396. }
  397. set { ViewState ["ShowNextPageButton"] = value; }
  398. }
  399. public bool ShowPreviousPageButton {
  400. get {
  401. object o = ViewState ["ShowPreviousPageButton"];
  402. if (o != null)
  403. return (bool) o;
  404. return true;
  405. }
  406. set { ViewState ["ShowPreviousPageButton"] = value; }
  407. }
  408. }
  409. }
  410. #endif