<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>Using the Built-In Pager with Custom Navigation Controls</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_oledb.cs" />
<script language="C#" runat="server">
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) bindGrid ( );
}
void bindGrid ( ) {
string query = "SELECT * FROM Products";
myGrid.DataSource = fetchData ( query, "gear" );
myGrid.DataBind ( );
}
void setPage ( Object src, GridViewPageEventArgs e ) {
// used by built-in pager.
// sets PageIndex to the page the user clicked.
myGrid.PageIndex = e.NewPageIndex;
// fetch and rebind the data.
bindGrid ( );
}
void setPageCustom ( Object src, CommandEventArgs e ) {
// used by custom paging UI
switch ( e.CommandName ) {
case ( "first" ) :
myGrid.PageIndex = 0;
break;
case ( "prev" ) :
if ( myGrid.PageIndex > 0 )
myGrid.PageIndex --;
break;
case ( "next" ) :
if ( myGrid.PageIndex < ( myGrid.PageCount - 1 ) )
myGrid.PageIndex ++;
break;
case ( "last" ) :
myGrid.PageIndex= ( myGrid.PageCount - 1 );
break;
}
bindGrid ( );
}
void setCustomPager ( Object src, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.Header ) {
TableCellCollection header = e.Row.Cells;
// remove extra cells in header
for ( int i = 0; i < 3; i++ )
header.RemoveAt ( 0 );
// set first header cell
header [ 0 ].ColumnSpan = 2;
header [ 0 ].Text = "Page " + ( myGrid.PageIndex + 1 ) +
" of " + myGrid.PageCount;
// set second header cell
header [ 1 ].ColumnSpan = 3;
header [ 1 ].HorizontalAlign = HorizontalAlign.Right;
header [ 1 ].Controls.AddAt ( 0, new Button ( ) );
Button btnFirst = ( Button ) header [ 1 ].Controls [ 0 ];
btnFirst.Text = "First";
btnFirst.CommandName = "first";
header [ 1 ].Controls.AddAt ( 1, new Button ( ) );
Button btnPrev = ( Button ) header [ 1 ].Controls [ 1 ];
btnPrev.Text = "Prev";
btnPrev.CommandName = "prev";
header [ 1 ].Controls.AddAt ( 2, new Button ( ) );
Button btnNext = ( Button ) header [ 1 ].Controls [ 2 ];
btnNext.Text = "Next";
btnNext.CommandName = "next";
header [ 1 ].Controls.AddAt ( 3, new Button ( ) );
Button btnLast = ( Button ) header [ 1 ].Controls [ 3 ];
btnLast.Text = "Last";
btnLast.CommandName = "last";
// set common button properties
foreach ( Control c in header [ 1 ].Controls ) {
( ( Button ) c ).Font.Bold = true;
( ( Button ) c ).BackColor = System.Drawing.Color.DarkRed;
( ( Button ) c ).ForeColor = System.Drawing.Color.Khaki;
( ( Button ) c ).Command += new CommandEventHandler ( setPageCustom );
}
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Using the Built-In Pager with <b>Custom Navigation Controls</b></h2></div>
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<asp:gridview id="myGrid" runat="server"
width="90%" cellpadding=5 gridlines="horizontal"
autogeneratecolumns=false
allowpaging pagesize=5
onRowCreated="setCustomPager"
onPageIndexChanging="setPage">
<headerstyle font-bold
backcolor="steelblue"
forecolor="snow" />
<pagersettings mode="numeric" />
<pagerstyle
backcolor="slategray" forecolor="khaki"
font-name="comic sans ms" font-bold
horizontalalign="center" />
<rowstyle font-size=10pt
backcolor="ghostwhite"
verticalalign="top" />
<columns>
<asp:templatefield>
<itemtemplate>
<asp:hyperlink runat="server"
navigateurl='<%# Eval ( "ProductID", "details_gear.aspx?id={0}" ) %>'>
<img width=75 border=0 runat="server"
src='<%# Eval ( "ProductID", "~/shared/images/gear/{0}.jpg" ) %>'
alt='<%# Eval ( "Model" ) %>' />
</asp:hyperlink>
</itemtemplate>
<itemstyle horizontalalign="center" />
</asp:templatefield>
<asp:boundfield datafield="Brand" />
<asp:boundfield datafield="Model" />
<asp:boundfield datafield="Description" />
<asp:boundfield datafield="Price"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
</form>
</div>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>