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.
32 using System.ComponentModel;
33 using System.Security.Permissions;
37 namespace System.Web.UI.WebControls
39 [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public class NextPreviousPagerField : DataPagerField
48 public NextPreviousPagerField ()
52 protected override void CopyProperties (DataPagerField newField)
54 base.CopyProperties (newField);
56 NextPreviousPagerField field = newField as NextPreviousPagerField;
60 field.ButtonCssClass = ButtonCssClass;
61 field.ButtonType = ButtonType;
62 field.FirstPageImageUrl = FirstPageImageUrl;
63 field.FirstPageText = FirstPageText;
64 field.LastPageImageUrl = LastPageImageUrl;
65 field.LastPageText = LastPageText;
66 field.NextPageImageUrl = NextPageImageUrl;
67 field.NextPageText = NextPageText;
68 field.PreviousPageImageUrl = PreviousPageImageUrl;
69 field.PreviousPageText = PreviousPageText;
70 field.ShowFirstPageButton = ShowFirstPageButton;
71 field.ShowLastPageButton = ShowLastPageButton;
72 field.ShowNextPageButton = ShowNextPageButton;
73 field.ShowPreviousPageButton = ShowPreviousPageButton;
76 // What's the fieldIndex used for?
77 public override void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows, int totalRowCount, int fieldIndex)
79 _startRowIndex = startRowIndex;
80 _maximumRows = maximumRows;
81 _totalRowCount = totalRowCount;
82 _fieldIndex = fieldIndex;
84 bool setPagePropertiesNeeded = false;
85 bool queryMode = GetQueryModeStartRowIndex (_totalRowCount, _maximumRows, ref _startRowIndex, ref setPagePropertiesNeeded);
86 bool enablePrevFirst = _startRowIndex >= _maximumRows;
87 bool enableNextLast = (_startRowIndex + _maximumRows) < _totalRowCount;
88 bool addNonBreakingSpace = RenderNonBreakingSpacesBetweenControls;
90 if (ShowFirstPageButton)
91 CreateButton (container, DataControlCommands.FirstPageCommandArgument, FirstPageText, FirstPageImageUrl, 0,
92 queryMode, enablePrevFirst, addNonBreakingSpace);
95 if (ShowPreviousPageButton) {
97 newPageNum = (_startRowIndex / _maximumRows) - 1;
99 CreateButton (container, DataControlCommands.PreviousPageCommandArgument, PreviousPageText, PreviousPageImageUrl, newPageNum,
100 queryMode, enablePrevFirst, addNonBreakingSpace);
103 if (ShowNextPageButton) {
105 newPageNum = (_startRowIndex + _maximumRows) / _maximumRows;
107 CreateButton (container, DataControlCommands.NextPageCommandArgument, NextPageText, NextPageImageUrl, newPageNum,
108 queryMode, enableNextLast, addNonBreakingSpace);
111 if (ShowLastPageButton) {
113 newPageNum = _totalRowCount / _maximumRows;
114 if ((_totalRowCount % _maximumRows) == 0)
118 CreateButton (container, DataControlCommands.LastPageCommandArgument, LastPageText, LastPageImageUrl, newPageNum,
119 queryMode, enableNextLast, addNonBreakingSpace);
122 if (setPagePropertiesNeeded)
123 DataPager.SetPageProperties (_startRowIndex, _maximumRows, true);
126 void CreateButton (DataPagerFieldItem container, string commandName, string text, string imageUrl, int pageNum,
127 bool queryMode, bool enabled, bool addNonBreakingSpace)
129 WebControl ctl = null;
133 HyperLink h = new HyperLink ();
135 h.ImageUrl = imageUrl;
137 h.NavigateUrl = GetQueryStringNavigateUrl (pageNum);
138 h.CssClass = ButtonCssClass;
141 if (!enabled && RenderDisabledButtonsAsLabels) {
142 Label l = new Label ();
146 switch (ButtonType) {
147 case ButtonType.Button:
148 Button btn = new Button ();
149 btn.CommandName = commandName;
154 case ButtonType.Link:
155 LinkButton lbtn = new LinkButton ();
156 lbtn.CommandName = commandName;
161 case ButtonType.Image:
162 ImageButton ibtn = new ImageButton ();
163 ibtn.CommandName = commandName;
164 ibtn.ImageUrl = imageUrl;
165 ibtn.AlternateText = text;
171 ctl.Enabled = enabled;
172 ctl.CssClass = ButtonCssClass;
178 container.Controls.Add (ctl);
179 if (addNonBreakingSpace)
180 container.Controls.Add (new LiteralControl (" "));
184 protected override DataPagerField CreateField ()
186 return new NextPreviousPagerField ();
189 public override bool Equals (object o)
191 NextPreviousPagerField field = o as NextPreviousPagerField;
195 // Compare using the properties that are copied in CopyProperties
196 if (field.ButtonCssClass != ButtonCssClass)
199 if (field.ButtonType != ButtonType)
202 if (field.FirstPageImageUrl != FirstPageImageUrl)
205 if (field.FirstPageText != FirstPageText)
208 if (field.LastPageImageUrl != LastPageImageUrl)
211 if (field.LastPageText != LastPageText)
214 if (field.NextPageImageUrl != NextPageImageUrl)
217 if (field.NextPageText != NextPageText)
220 if (field.PreviousPageImageUrl != PreviousPageImageUrl)
223 if (field.PreviousPageText != PreviousPageText)
226 if (field.ShowFirstPageButton != ShowFirstPageButton)
229 if (field.ShowLastPageButton != ShowLastPageButton)
232 if (field.ShowNextPageButton != ShowNextPageButton)
235 if (field.ShowPreviousPageButton != ShowPreviousPageButton)
241 public override int GetHashCode ()
245 // Base the calculation on the properties that are copied in CopyProperties
246 ret |= ButtonCssClass.GetHashCode ();
247 ret |= ButtonType.GetHashCode ();
248 ret |= FirstPageImageUrl.GetHashCode ();
249 ret |= FirstPageText.GetHashCode ();
250 ret |= LastPageImageUrl.GetHashCode ();
251 ret |= LastPageText.GetHashCode ();
252 ret |= NextPageImageUrl.GetHashCode ();
253 ret |= NextPageText.GetHashCode ();
254 ret |= PreviousPageImageUrl.GetHashCode ();
255 ret |= PreviousPageText.GetHashCode ();
256 ret |= ShowFirstPageButton.GetHashCode ();
257 ret |= ShowLastPageButton.GetHashCode ();
258 ret |= ShowNextPageButton.GetHashCode ();
259 ret |= ShowPreviousPageButton.GetHashCode ();
264 public override void HandleEvent (CommandEventArgs e)
266 string commandName = e.CommandName;
267 int newStartIndex = -1;
268 int pageSize = DataPager.PageSize;
270 if (String.Compare (commandName, DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0)
272 else if (String.Compare (commandName, DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
273 int lastPageMod = _totalRowCount % pageSize;
274 if (lastPageMod == 0)
275 newStartIndex = _totalRowCount - pageSize;
277 newStartIndex = _totalRowCount - lastPageMod;
278 } else if (String.Compare (commandName, DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
279 newStartIndex = _startRowIndex + pageSize;
280 if (newStartIndex > _totalRowCount)
281 newStartIndex = _totalRowCount - pageSize;
282 } else if (String.Compare (commandName, DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase) == 0) {
283 newStartIndex = _startRowIndex - pageSize;
284 if (newStartIndex < 0)
288 if (newStartIndex != -1)
289 DataPager.SetPageProperties (newStartIndex, pageSize, true);
292 public string ButtonCssClass {
294 string s = ViewState ["ButtonCssClass"] as string;
301 set { ViewState ["ButtonCssClass"] = value; }
304 public ButtonType ButtonType {
306 object o = ViewState ["ButtonType"];
308 return (ButtonType) o;
310 return ButtonType.Link;
313 set { ViewState ["ButtonType"] = value; }
316 public string FirstPageImageUrl {
318 string s = ViewState ["FirstPageImageUrl"] as string;
325 set { ViewState ["FirstPageImageUrl"] = value; }
328 public string FirstPageText {
330 string s = ViewState ["FirstPageText"] as string;
337 set { ViewState ["FirstPageText"] = value; }
340 public string LastPageImageUrl {
342 string s = ViewState ["LastPageImageUrl"] as string;
349 set { ViewState ["LastPageImageUrl"] = value; }
352 public string LastPageText {
354 string s = ViewState ["LastPageText"] as string;
361 set { ViewState ["LastPageText"] = value; }
364 public string NextPageImageUrl {
366 string s = ViewState ["NextPageImageUrl"] as string;
373 set { ViewState ["NextPageImageUrl"] = value; }
376 public string NextPageText {
378 string s = ViewState ["NextPageText"] as string;
385 set { ViewState ["NextPageText"] = value; }
388 public string PreviousPageImageUrl {
390 string s = ViewState ["PreviousPageImageUrl"] as string;
397 set { ViewState ["PreviousPageImageUrl"] = value; }
400 public string PreviousPageText {
402 string s = ViewState ["PreviousPageText"] as string;
409 set { ViewState ["PreviousPageText"] = value; }
412 public bool RenderDisabledButtonsAsLabels {
414 object o = ViewState ["RenderDisabledButtonsAsLabels"];
421 set { ViewState ["RenderDisabledButtonsAsLabels"] = value; }
424 public bool RenderNonBreakingSpacesBetweenControls {
426 object o = ViewState ["RenderNonBreakingSpacesBetweenControls"];
433 set { ViewState ["RenderNonBreakingSpacesBetweenControls"] = value; }
436 public bool ShowFirstPageButton {
438 object o = ViewState ["ShowFirstPageButton"];
445 set { ViewState ["ShowFirstPageButton"] = value; }
448 public bool ShowLastPageButton {
450 object o = ViewState ["ShowLastPageButton"];
457 set { ViewState ["ShowLastPageButton"] = value; }
460 public bool ShowNextPageButton {
462 object o = ViewState ["ShowNextPageButton"];
469 set { ViewState ["ShowNextPageButton"] = value; }
472 public bool ShowPreviousPageButton {
474 object o = ViewState ["ShowPreviousPageButton"];
481 set { ViewState ["ShowPreviousPageButton"] = value; }