<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>DataGrid Custom Paging Controls Example</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 ( );
myGrid.PagerStyle.Visible = showPager.Checked;
}
void setPageCustom ( Object src, CommandEventArgs e ) {
// used by custom paging UI
switch ( e.CommandName ) {
case ( "first" ) :
myGrid.CurrentPageIndex = 0;
break;
case ( "prev" ) :
if ( myGrid.CurrentPageIndex > 0 )
myGrid.CurrentPageIndex --;
break;
case ( "next" ) :
if ( myGrid.CurrentPageIndex < ( myGrid.PageCount - 1 ) )
myGrid.CurrentPageIndex ++;
break;
case ( "last" ) :
myGrid.CurrentPageIndex= ( myGrid.PageCount - 1 );
break;
}
bindGrid ( );
}
void setPage ( Object src, DataGridPageChangedEventArgs e ) {
// used by built-in pager.
// sets CurrentPageIndex to the page the user clicked.
myGrid.CurrentPageIndex = e.NewPageIndex;
// fetch and rebind the data.
bindGrid ( );
}
void bindGrid ( ) {
string query = "SELECT * FROM products";
myGrid.DataSource = fetchData ( query, "gear" );
myGrid.DataBind ( );
lblTracker.Text = "Page " + ( myGrid.CurrentPageIndex + 1 ) +
" of " + myGrid.PageCount;
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2><span class="hilite">DataGrid</span> Custom Paging Controls Example</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<table cellpadding=5 width=90%>
<tr>
<td><b><asp:label id="lblTracker" runat="server" /></b></td>
<td align="right">
<asp:button id="btnFirst" runat="server"
text="First"
commandname="first"
font-bold
backcolor="navy"
forecolor="lime"
onCommand="setPageCustom" />
<asp:button id="btnPrev" runat="server"
text="Back"
commandname="prev"
font-bold
backcolor="navy"
forecolor="lime"
onCommand="setPageCustom" />
<asp:button id="btnNext" runat="server"
text="Next"
commandname="next"
font-bold
backcolor="navy"
forecolor="lime"
onCommand="setPageCustom" />
<asp:button id="btnLast" runat="server"
text="Last"
commandname="last"
font-bold
backcolor="navy"
forecolor="lime"
onCommand="setPageCustom" />
</td></tr>
</table>
<asp:datagrid id="myGrid" runat="server"
width="90%" cellpadding=5 font-size="10pt"
headerstyle-backcolor="lightsteelblue"
headerstyle-font-bold
itemstyle-verticalalign="top"
autogeneratecolumns=false
allowpaging pagesize=5
onPageIndexChanged="setPage">
<pagerstyle
mode="numericpages"
backcolor="slategray" forecolor="khaki"
font-name="comic sans ms" font-size=10pt font-bold
horizontalalign="center" />
<columns>
<asp:templatecolumn>
<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>
</asp:templatecolumn>
<asp:boundcolumn headertext="Brand"
datafield="Brand" />
<asp:boundcolumn headertext="Model"
datafield="Model" />
<asp:boundcolumn headertext="Description"
datafield="Description" />
<asp:boundcolumn headertext="Price"
datafield="Price"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right" />
</columns>
</asp:datagrid>
<p>
<asp:checkbox id="showPager" runat="server"
text="Show built-in pager" autopostback />
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>