public class ReversedNumericPagerField: DataPagerField
{
private int _startRowIndex;
private int _maximumRows;
private int _totalRowCount;
public ReversedNumericPagerField ()
{
}
}
* This source code was highlighted with Source Code Highlighter .
protected override DataPagerField CreateField ()
{
return new ReversedNumericPagerField ();
}
* This source code was highlighted with Source Code Highlighter .
public override void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows, int totalRowCount, int fieldIndex)
{
_startRowIndex = startRowIndex;
_maximumRows = maximumRows;
_totalRowCount = totalRowCount;
CreateDataPagersForCommand (container, fieldIndex);
} * This source code was highlighted with Source Code Highlighter .
private void CreateDataPagersForCommand (DataPagerFieldItem container, int fieldIndex)
{
int currentPageIndex = _startRowIndex / _maximumRows;
int firstButtonIndex = (_startRowIndex / (ButtonCount * _maximumRows)) * ButtonCount;
int lastButtonIndex = firstButtonIndex + ButtonCount - 1;
int lastRecordIndex = ((lastButtonIndex + 1) * _maximumRows) - 1;
int totalPages = ( int ) Math .Ceiling (( double ) _totalRowCount / _maximumRows);
for ( int i = 0; i <ButtonCount && _totalRowCount> ((i + firstButtonIndex) * _maximumRows); i ++)
{
if (i + firstButtonIndex == currentPageIndex)
{
Label pageNumber = new Label ();
pageNumber .Text = (totalPages - i - firstButtonIndex) .ToString ();
container.Controls.Add (pageNumber);
}
else
{
container.Controls.Add (CreateNumericButton ((totalPages - i - firstButtonIndex) .ToString (), fieldIndex.ToString (), (i + firstButtonIndex) .ToString ()));
}
container.Controls.Add ( new LiteralControl ( "" ));
}
}
* This source code was highlighted with Source Code Highlighter .
private Control CreateNumericButton ( string buttonText, string commandArgument, string commandName)
{
LinkButton button = new LinkButton ();
button.Text = buttonText;
button.CommandName = commandName;
button.CommandArgument = commandArgument;
return button as Control;
}
* This source code was highlighted with Source Code Highlighter .
public override void HandleEvent (CommandEventArgs e)
{
int newStartRowIndex = -1;
int currentPageIndex = _startRowIndex / DataPager.PageSize;
int firstButtonIndex = (_startRowIndex / (ButtonCount * DataPager.PageSize)) * ButtonCount;
int lastButtonIndex = firstButtonIndex + ButtonCount - 1;
int lastRecordIndex = ((lastButtonIndex + 1) * DataPager.PageSize) - 1;
if ( String .Equals (e.CommandName, DataControlCommands.PreviousPageCommandArgument))
{
newStartRowIndex = (firstButtonIndex - 1) * DataPager.PageSize;
if (newStartRowIndex <0)
{
newStartRowIndex = 0;
}
}
else if ( String .Equals (e.CommandName, DataControlCommands.NextPageCommandArgument))
{
newStartRowIndex = lastRecordIndex + 1;
if (newStartRowIndex> _totalRowCount)
{
newStartRowIndex = _totalRowCount - DataPager.PageSize;
}
}
else
{
int pageIndex = Convert .ToInt32 (e.CommandName, CultureInfo.InvariantCulture);
newStartRowIndex = pageIndex * DataPager.PageSize;
}
if (newStartRowIndex! = -1)
{
DataPager.SetPageProperties (newStartRowIndex, DataPager.PageSize, true );
}
}
* This source code was highlighted with Source Code Highlighter .
public class ReversedNumericPagerField: DataPagerField
{
private int _startRowIndex;
private int _maximumRows;
private int _totalRowCount;
public ReversedNumericPagerField ()
{
}
public int ButtonCount
{
get
{
object o = ViewState [ "ButtonCount" ];
if (o! = null )
{
return ( int ) o;
}
return 5;
}
set
{
if ( value <1)
{
throw new ArgumentOutOfRangeException ( "value" );
}
if ( value ! = ButtonCount)
{
ViewState [ "ButtonCount" ] = value ;
OnFieldChanged ();
}
}
}
public ButtonType ButtonType
{
get
{
object o = ViewState [ "ButtonType" ];
if (o! = null )
return (ButtonType) o;
return ButtonType.Link;
}
set
{
if ( value <ButtonType.Button || value > ButtonType.Link)
{
throw new ArgumentOutOfRangeException ( "value" );
}
if ( value ! = ButtonType)
{
ViewState [ "ButtonType" ] = value ;
OnFieldChanged ();
}
}
}
public string CurrentPageLabelCssClass
{
get
{
object o = ViewState [ "CurrentPageLabelCssClass" ];
if (o! = null )
{
return ( string ) o;
}
return String .Empty;
}
set
{
if ( value ! = CurrentPageLabelCssClass)
{
ViewState [ "CurrentPageLabelCssClass" ] = value ;
OnFieldChanged ();
}
}
}
public string NextPageImageUrl
{
get
{
object o = ViewState [ "NextPageImageUrl" ];
if (o! = null )
{
return ( string ) o;
}
return String .Empty;
}
set
{
if ( value ! = NextPageImageUrl)
{
ViewState [ "NextPageImageUrl" ] = value ;
OnFieldChanged ();
}
}
}
public string NextPageText
{
get
{
object o = ViewState [ "NextPageText" ];
if (o! = null )
{
return ( string ) o;
}
return "→" ;
}
set
{
if ( value ! = NextPageText)
{
ViewState [ "NextPageText" ] = value ;
OnFieldChanged ();
}
}
}
public string NextPreviousButtonCssClass
{
get
{
object o = ViewState [ "NextPreviousButtonCssClass" ];
if (o! = null )
{
return ( string ) o;
}
return String .Empty;
}
set
{
if ( value ! = NextPreviousButtonCssClass)
{
ViewState [ "NextPreviousButtonCssClass" ] = value ;
OnFieldChanged ();
}
}
}
public string NumericButtonCssClass
{
get
{
object o = ViewState [ "NumericButtonCssClass" ];
if (o! = null )
{
return ( string ) o;
}
return String .Empty;
}
set
{
if ( value ! = NumericButtonCssClass)
{
ViewState [ "NumericButtonCssClass" ] = value ;
OnFieldChanged ();
}
}
}
public string PreviousPageImageUrl
{
get
{
object o = ViewState [ "PreviousPageImageUrl" ];
if (o! = null )
{
return ( string ) o;
}
return String .Empty;
}
set
{
if ( value ! = PreviousPageImageUrl)
{
ViewState [ "PreviousPageImageUrl" ] = value ;
OnFieldChanged ();
}
}
}
public string PreviousPageText
{
get
{
object o = ViewState [ "PreviousPageText" ];
if (o! = null )
{
return ( string ) o;
}
return "←" ;
}
set
{
if ( value ! = PreviousPageText)
{
ViewState [ "PreviousPageText" ] = value ;
OnFieldChanged ();
}
}
}
public bool RenderNonBreakingSpacesBetweenControls
{
get
{
object o = ViewState [ "RenderNonBreakingSpacesBetweenControls" ];
if (o! = null )
{
return ( bool ) o;
}
return true ;
}
set
{
if ( value ! = RenderNonBreakingSpacesBetweenControls)
{
ViewState [ "RenderNonBreakingSpacesBetweenControls" ] = value ;
OnFieldChanged ();
}
}
}
private void AddNonBreakingSpace (DataPagerFieldItem container)
{
if (RenderNonBreakingSpacesBetweenControls)
{
container.Controls.Add ( new LiteralControl ( "" ));
}
}
protected override DataPagerField CreateField ()
{
return new ReversedNumericPagerField ();
}
public override void HandleEvent (CommandEventArgs e)
{
int newStartRowIndex = -1;
int currentPageIndex = _startRowIndex / DataPager.PageSize;
int firstButtonIndex = (_startRowIndex / (ButtonCount * DataPager.PageSize)) * ButtonCount;
int lastButtonIndex = firstButtonIndex + ButtonCount - 1;
int lastRecordIndex = ((lastButtonIndex + 1) * DataPager.PageSize) - 1;
if ( String .Equals (e.CommandName, DataControlCommands.PreviousPageCommandArgument))
{
newStartRowIndex = (firstButtonIndex - 1) * DataPager.PageSize;
if (newStartRowIndex <0)
{
newStartRowIndex = 0;
}
}
else if ( String .Equals (e.CommandName, DataControlCommands.NextPageCommandArgument))
{
newStartRowIndex = lastRecordIndex + 1;
if (newStartRowIndex> _totalRowCount)
{
newStartRowIndex = _totalRowCount - DataPager.PageSize;
}
}
else
{
int pageIndex = Convert .ToInt32 (e.CommandName, CultureInfo.InvariantCulture);
newStartRowIndex = pageIndex * DataPager.PageSize;
}
if (newStartRowIndex! = -1)
{
DataPager.SetPageProperties (newStartRowIndex, DataPager.PageSize, true );
}
}
private Control CreateNumericButton ( string buttonText, string commandArgument, string commandName)
{
IButtonControl button;
switch (ButtonType)
{
case ButtonType.Button:
button = new Button ();
break ;
case ButtonType.Link:
default :
button = new LinkButton ();
break ;
}
button.Text = buttonText;
button.CommandName = commandName;
button.CommandArgument = commandArgument;
WebControl webControl = button as WebControl;
if (webControl! = null &&! String .IsNullOrEmpty (NumericButtonCssClass))
{
webControl.CssClass = NumericButtonCssClass;
}
return button as Control;
}
private Control CreateNextPrevButton ( string buttonText, string commandName, string commandArgument, string imageUrl)
{
IButtonControl button;
switch (ButtonType)
{
case ButtonType.Link:
button = new LinkButton ();
break ;
case ButtonType.Button:
button = new Button ();
break ;
case ButtonType.Image:
default :
button = new ImageButton ();
((ImageButton) button) .ImageUrl = imageUrl;
((ImageButton) button) .AlternateText = HttpUtility.HtmlDecode (buttonText);
break ;
}
button.Text = buttonText;
button.CommandName = commandName;
button.CommandArgument = commandArgument;
WebControl webControl = button as WebControl;
if (webControl! = null &&! String .IsNullOrEmpty (NextPreviousButtonCssClass))
{
webControl.CssClass = NextPreviousButtonCssClass;
}
return button as Control;
}
public override void CreateDataPagers (DataPagerFieldItem container, int startRowIndex, int maximumRows, int totalRowCount, int fieldIndex)
{
_startRowIndex = startRowIndex;
_maximumRows = maximumRows;
_totalRowCount = totalRowCount;
CreateDataPagersForCommand (container, fieldIndex);
}
private void CreateDataPagersForCommand (DataPagerFieldItem container, int fieldIndex)
{
int currentPageIndex = _startRowIndex / _maximumRows;
int firstButtonIndex = (_startRowIndex / (ButtonCount * _maximumRows)) * ButtonCount;
int lastButtonIndex = firstButtonIndex + ButtonCount - 1;
int lastRecordIndex = ((lastButtonIndex + 1) * _maximumRows) - 1;
int totalPages = ( int ) Math .Ceiling (( double ) _totalRowCount / _maximumRows);
if (firstButtonIndex! = 0)
{
container.Controls.Add (CreateNextPrevButton (PreviousPageText, DataControlCommands.PreviousPageCommandArgument, fieldIndex.ToString (CultureInfo.InvariantCulture), PreviousPageImageUrl));
AddNonBreakingSpace (container);
}
for ( int i = 0; i <ButtonCount && _totalRowCount> ((i + firstButtonIndex) * _maximumRows); i ++)
{
if (i + firstButtonIndex == currentPageIndex)
{
Label pageNumber = new Label ();
pageNumber.Text = (totalPages - i - firstButtonIndex) .ToString (CultureInfo. InvariantCulture);
if (! String .IsNullOrEmpty (CurrentPageLabelCssClass))
{
pageNumber.CssClass = CurrentPageLabelCssClass;
}
container.Controls.Add (pageNumber);
}
else
{
container.
}
AddNonBreakingSpace (container);
}
if (lastRecordIndex <_totalRowCount - 1)
{
AddNonBreakingSpace (container);
container.Controls.Add (CreateNextPrevButton (NextPageText, DataControlCommands.NextPageCommandArgument, fieldIndex.ToString (CultureInfo.InvariantCulture), NextPageImageUrl));
AddNonBreakingSpace (container);
}
}
} * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/28441/
All Articles