2 // System.Web.UI.WebControls.NextPreviousPagerField
5 // Marek Habersack (mhabersack@novell.com)
7 // (C) 2007-2008 Novell, Inc
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.ComponentModel;
32 using System.Security.Permissions;
36 namespace System.Web.UI.WebControls
38 [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public class NextPreviousPagerField : DataPagerField
47 public NextPreviousPagerField ()
51 protected override void CopyProperties (DataPagerField newField)
53 base.CopyProperties (newField);
55 NextPreviousPagerField field = newField as NextPreviousPagerField;
59 field.ButtonCssClass = ButtonCssClass;
60 field.ButtonType = ButtonType;
61 field.FirstPageImageUrl = FirstPageImageUrl;
62 field.FirstPageText = FirstPageText;
63 field.LastPageImageUrl = LastPageImageUrl;
64 field.LastPageText = LastPageText;
65 field.NextPageImageUrl = NextPageImageUrl;
66 field.NextPageText = NextPageText;
67 field.PreviousPageImageUrl = PreviousPageImageUrl;
68 field.PreviousPageText = PreviousPageText;
69 field.ShowFirstPageButton = ShowFirstPageButton;
70 field.ShowLastPageButton = ShowLastPageButton;
71 field.ShowNextPageButton = ShowNextPageButton;
72 field.ShowPreviousPageButton = ShowPreviousPageButton;
75 // What's the fieldIndex used for?
76 public override void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows, int totalRowCount, int fieldIndex)
78 _startRowIndex = startRowIndex;
79 _maximumRows = maximumRows;
80 _totalRowCount = totalRowCount;
81 _fieldIndex = fieldIndex;
83 bool setPagePropertiesNeeded = false;
84 bool queryMode = GetQueryModeStartRowIndex (_totalRowCount, _maximumRows, ref _startRowIndex, ref setPagePropertiesNeeded);
85 bool enablePrevFirst = _startRowIndex >= _maximumRows;
86 bool enableNextLast = (_startRowIndex + _maximumRows) < _totalRowCount;
87 bool addNonBreakingSpace = RenderNonBreakingSpacesBetweenControls;
89 if (ShowFirstPageButton)
90 CreateButton (container, DataControlCommands.FirstPageCommandArgument, FirstPageText, FirstPageImageUrl, 0,
91 queryMode, enablePrevFirst, addNonBreakingSpace);
94 if (ShowPreviousPageButton) {
96 newPageNum = (_startRowIndex / _maximumRows) - 1;
98 CreateButton (container, DataControlCommands.PreviousPageCommandArgument, PreviousPageText, PreviousPageImageUrl, newPageNum,
99 queryMode, enablePrevFirst, addNonBreakingSpace);
102 if (ShowNextPageButton) {
104 newPageNum = (_startRowIndex + _maximumRows) / _maximumRows;
106 CreateButton (container, DataControlCommands.NextPageCommandArgument, NextPageText, NextPageImageUrl, newPageNum,
107 queryMode, enableNextLast, addNonBreakingSpace);
110 if (ShowLastPageButton) {
112 newPageNum = _totalRowCount / _maximumRows;
113 if ((_totalRowCount % _maximumRows) == 0)
117 CreateButton (container, DataControlCommands.LastPageCommandArgument, LastPageText, LastPageImageUrl, newPageNum,
118 queryMode, enableNextLast, addNonBreakingSpace);
121 if (setPagePropertiesNeeded)
122 DataPager.SetPageProperties (_startRowIndex, _maximumRows, true);
125 void CreateButton (DataPagerFieldItem container, string commandName, string text, string imageUrl, int pageNum,
126 bool queryMode, bool enabled, bool addNonBreakingSpace)
128 WebControl ctl = null;
132 HyperLink h = new HyperLink ();
134 h.ImageUrl = imageUrl;
136 h.NavigateUrl = GetQueryStringNavigateUrl (pageNum);
137 h.CssClass = ButtonCssClass;
140 if (!enabled && RenderDisabledButtonsAsLabels) {
141 Label l = new Label ();
145 switch (ButtonType) {
146 case ButtonType.Button:
147 Button btn = new Button ();
148 btn.CommandName = commandName;
153 case ButtonType.Link:
154 LinkButton lbtn = new LinkButton ();
155 lbtn.CommandName = commandName;
160 case ButtonType.Image:
161 ImageButton ibtn = new ImageButton ();
162 ibtn.CommandName = commandName;
163 ibtn.ImageUrl = imageUrl;
164 ibtn.AlternateText = text;
170 ctl.Enabled = enabled;
171 ctl.CssClass = ButtonCssClass;
177 container.Controls.Add (ctl);
178 if (addNonBreakingSpace)
179 container.Controls.Add (new LiteralControl (" "));
183 protected override DataPagerField CreateField ()
185 return new NextPreviousPagerField ();
188 public override bool Equals (object o)
190 NextPreviousPagerField field = o as NextPreviousPagerField;
194 // Compare using the properties that are copied in CopyProperties
195 if (field.ButtonCssClass != ButtonCssClass)
198 if (field.ButtonType != ButtonType)
201 if (field.FirstPageImageUrl != FirstPageImageUrl)
204 if (field.FirstPageText != FirstPageText)
207 if (field.LastPageImageUrl != LastPageImageUrl)
210 if (field.LastPageText != LastPageText)
213 if (field.NextPageImageUrl != NextPageImageUrl)
216 if (field.NextPageText != NextPageText)
219 if (field.PreviousPageImageUrl != PreviousPageImageUrl)
222 if (field.PreviousPageText != PreviousPageText)
225 if (field.ShowFirstPageButton != ShowFirstPageButton)
228 if (field.ShowLastPageButton != ShowLastPageButton)
231 if (field.ShowNextPageButton != ShowNextPageButton)
234 if (field.ShowPreviousPageButton != ShowPreviousPageButton)
240 public override int GetHashCode ()
244 // Base the calculation on the properties that are copied in CopyProperties
245 ret |= ButtonCssClass.GetHashCode ();
246 ret |= ButtonType.GetHashCode ();
247 ret |= FirstPageImageUrl.GetHashCode ();
248 ret |= FirstPageText.GetHashCode ();
249 ret |= LastPageImageUrl.GetHashCode ();
250 ret |= LastPageText.GetHashCode ();
251 ret |= NextPageImageUrl.GetHashCode ();
252 ret |= NextPageText.GetHashCode ();
253 ret |= PreviousPageImageUrl.GetHashCode ();
254 ret |= PreviousPageText.GetHashCode ();
255 ret |= ShowFirstPageButton.GetHashCode ();
256 ret |= ShowLastPageButton.GetHashCode ();
257 ret |= ShowNextPageButton.GetHashCode ();
258 ret |= ShowPreviousPageButton.GetHashCode ();
263 public override void HandleEvent (CommandEventArgs e)
265 string commandName = e.CommandName;
266 int newStartIndex = -1;
267 int pageSize = DataPager.PageSize;
269 if (String.Compare (commandName, DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0)
271 else if (String.Compare (commandName, DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
272 int lastPageMod = _totalRowCount % pageSize;
273 if (lastPageMod == 0)
274 newStartIndex = _totalRowCount - pageSize;
276 newStartIndex = _totalRowCount - lastPageMod;
277 } else if (String.Compare (commandName, DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
278 newStartIndex = _startRowIndex + pageSize;
279 if (_totalRowCount >= 0 && newStartIndex > _totalRowCount)
280 newStartIndex = _totalRowCount - pageSize;
281 } else if (String.Compare (commandName, DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
282 newStartIndex = _startRowIndex - pageSize;
283 if (newStartIndex < 0)
287 if (newStartIndex >= 0)
288 DataPager.SetPageProperties (newStartIndex, pageSize, true);
291 public string ButtonCssClass {
293 string s = ViewState ["ButtonCssClass"] as string;
300 set { ViewState ["ButtonCssClass"] = value; }
303 public ButtonType ButtonType {
305 object o = ViewState ["ButtonType"];
307 return (ButtonType) o;
309 return ButtonType.Link;
312 set { ViewState ["ButtonType"] = value; }
315 public string FirstPageImageUrl {
317 string s = ViewState ["FirstPageImageUrl"] as string;
324 set { ViewState ["FirstPageImageUrl"] = value; }
327 public string FirstPageText {
329 string s = ViewState ["FirstPageText"] as string;
336 set { ViewState ["FirstPageText"] = value; }
339 public string LastPageImageUrl {
341 string s = ViewState ["LastPageImageUrl"] as string;
348 set { ViewState ["LastPageImageUrl"] = value; }
351 public string LastPageText {
353 string s = ViewState ["LastPageText"] as string;
360 set { ViewState ["LastPageText"] = value; }
363 public string NextPageImageUrl {
365 string s = ViewState ["NextPageImageUrl"] as string;
372 set { ViewState ["NextPageImageUrl"] = value; }
375 public string NextPageText {
377 string s = ViewState ["NextPageText"] as string;
384 set { ViewState ["NextPageText"] = value; }
387 public string PreviousPageImageUrl {
389 string s = ViewState ["PreviousPageImageUrl"] as string;
396 set { ViewState ["PreviousPageImageUrl"] = value; }
399 public string PreviousPageText {
401 string s = ViewState ["PreviousPageText"] as string;
408 set { ViewState ["PreviousPageText"] = value; }
411 public bool RenderDisabledButtonsAsLabels {
413 object o = ViewState ["RenderDisabledButtonsAsLabels"];
420 set { ViewState ["RenderDisabledButtonsAsLabels"] = value; }
423 public bool RenderNonBreakingSpacesBetweenControls {
425 object o = ViewState ["RenderNonBreakingSpacesBetweenControls"];
432 set { ViewState ["RenderNonBreakingSpacesBetweenControls"] = value; }
435 public bool ShowFirstPageButton {
437 object o = ViewState ["ShowFirstPageButton"];
444 set { ViewState ["ShowFirstPageButton"] = value; }
447 public bool ShowLastPageButton {
449 object o = ViewState ["ShowLastPageButton"];
456 set { ViewState ["ShowLastPageButton"] = value; }
459 public bool ShowNextPageButton {
461 object o = ViewState ["ShowNextPageButton"];
468 set { ViewState ["ShowNextPageButton"] = value; }
471 public bool ShowPreviousPageButton {
473 object o = ViewState ["ShowPreviousPageButton"];
480 set { ViewState ["ShowPreviousPageButton"] = value; }