The code sample below was stripped out of a Silverlight 2.0 line of business application.
You can adapt the numbering scheme
to support multi-lingual buttons. Plus, you can also write versions that use something
other than a button control for the paging button.
For those of you haven't had much experience writing queries for SQL Server paging,
this article will also prove helpful.
Download Source Code
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace EggHeadCafe.UI
{
public static class Pager
{
#region Add Pager Button
private static void AddPagerButton(StackPanel panel,
RoutedEventHandler clickEventHandler,
PagerButton pagerButton)
{
var
button = new Button();
button.Margin
= new Thickness(0, 0, 0, 0);
button.Content
= pagerButton.Text;
button.Tag
= pagerButton;
button.Click
+= clickEventHandler;
if (!pagerButton.IsSelected)
{
button.Style
= (Style)StyleController.FindResource("ButtonStyle");
}
else
{
button.Style
= (Style)StyleController.FindResource("ButtonStyleSelected");
}
panel.Children.Add(button);
}
#endregion
#region Get Starting Row Index
public static int GetStartingRowIndex(int currentPage, int maxRowsPerPage)
{
if (currentPage <= 1) return 1;
int idx = 1;
for (int i = 1; i < currentPage; i++)
{
idx
+= maxRowsPerPage;
}
return idx;
}
#endregion
#region Load Pager Buttons
public static void LoadPagerButtons(StackPanel panel,
RoutedEventHandler clickEventHandler,
int currentPage,
Int64 maxRows,
int maxRowsPerPage,
int maxNumberBoxesPerPage)
{
var
sb = new StringBuilder();
double maxPages = 1;
int i = 1;
int start = 1;
int end = maxNumberBoxesPerPage;
if (currentPage == 0) currentPage = 1;
panel.Children.Clear();
panel.Visibility
= Visibility.Visible;
if (maxRows >= maxRowsPerPage)
{
double tempMaxPages = (double)maxRows / (double)maxRowsPerPage;
double tempMaxPages2 = Math.Round(tempMaxPages, 0);
if (tempMaxPages > tempMaxPages2) { maxPages = (int)tempMaxPages + 1; }
else { maxPages = tempMaxPages2; }
}
if (maxPages < 1) maxPages = 1;
if (currentPage > 1)
{
AddPagerButton(panel,
clickEventHandler, new PagerButton(maxRows, 1, maxRowsPerPage, "First", false));
AddPagerButton(panel,
clickEventHandler, new PagerButton(maxRows,currentPage - 1,maxRowsPerPage, "Previous", false));
}
if (currentPage >= maxNumberBoxesPerPage) start = currentPage - 3;
while (i <= maxNumberBoxesPerPage)
{
if (start == currentPage)
{
AddPagerButton(panel,
clickEventHandler, new PagerButton(maxRows, start, maxRowsPerPage, start.ToString(), true));
}
else
{
AddPagerButton(panel,
clickEventHandler, new PagerButton(maxRows, start, maxRowsPerPage, start.ToString(), false));
}
start++;
i++;
if (start > maxPages) break;
}
if (currentPage < maxPages)
{
AddPagerButton(panel,
clickEventHandler,new PagerButton(maxRows, currentPage + 1, maxRowsPerPage, "Next", false));
AddPagerButton(panel,
clickEventHandler, new PagerButton(maxRows, Convert.ToInt32(maxPages), maxRowsPerPage, "Last", false));
}
if (panel.Children.Count < 2) panel.Children.Clear();
}
#endregion
}
public class PagerButton
{
public PagerButton(Int64 totalRows, Int32 currentPage, Int32 maximumRowsPerPage, string text, bool isSelected)
{
TotalRows
= totalRows;
CurrentPage
= currentPage;
MaximumRowsPerPage
= maximumRowsPerPage;
Text
= text;
IsSelected
= isSelected;
}
public Int64 TotalRows { get; set; }
public Int32 CurrentPage { get; set; }
public Int32 MaximumRowsPerPage { get; set; }
public string Text { get; set; }
public bool IsSelected { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EggHeadCafe.UI;
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
int _rowCount = 1000;
int _maxRowsPerGrid = 10;
int _maxNumberButtons = 20;
public Page()
{
StyleController.ApplicationRootElement
= (FrameworkElement)App.Current.RootVisual;
StyleController.ResourceDictionary
= App.Current.Resources;
InitializeComponent();
Pager.LoadPagerButtons(this.PagerButtons,
new RoutedEventHandler(PagerButtonClick),
0,
_rowCount,
_maxRowsPerGrid,
_maxNumberButtons);
}
private void PagerButtonClick(object sender, RoutedEventArgs e)
{
var
button = sender as Button;
if ((button == null) || (button.Tag == null)) return;
var
page = button.Tag as PagerButton;
if (page == null) return;
Pager.LoadPagerButtons(this.PagerButtons,
new RoutedEventHandler(PagerButtonClick),
page.CurrentPage,
_rowCount,
_maxRowsPerGrid,
_maxNumberButtons);
}
}
}