<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>Custom Paging Using Primary Key Values</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_oledb.cs" />
<script language="C#" runat="server">
int rowCount;
string category, query;
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
query = "SELECT DISTINCT Type FROM Products";
typesList.DataSource = fetchReader ( query, "gear" );
typesList.DataBind ( );
// initialize selection
getSubTypes ( null,null );
}
else category = subtypesList.SelectedValue;
}
void getSubTypes ( Object src, EventArgs e ) {
query = "SELECT DISTINCT SubType FROM Products where Type='" +
typesList.SelectedValue + "'";
subtypesList.DataSource = fetchReader ( query, "gear" );
subtypesList.DataBind ( );
// initialize selection
subtypesList.SelectedIndex = 0;
category = subtypesList.SelectedValue;
getFirstPage ( null,null );
}
void getFirstPage ( Object src, EventArgs e ) {
// get total records count of query
myGrid.VirtualItemCount = countRows ( );
// reset paging variables
myGrid.CurrentPageIndex = 0;
ViewState [ "topID" ] = "";
ViewState [ "endID" ] = "";
// get first set of records
query = "SELECT top " + myGrid.PageSize +
" * FROM Products where SubType='" + category + "' order by ProductID";
bindGrid ( );
}
void setPage ( Object src, DataGridPageChangedEventArgs e ) {
if ( e.NewPageIndex == myGrid.CurrentPageIndex + 1 )
// get next set of records
query = "SELECT top " + myGrid.PageSize +
" * FROM Products where SubType='" + category + "' and ProductID > '" +
ViewState [ "endID" ] + "' order by ProductID";
else // get previous set of records
query = "SELECT top " + myGrid.PageSize +
" * FROM Products where SubType='" + category + "' and ProductID < '" +
ViewState [ "topID" ] + "' order by ProductID desc";
myGrid.CurrentPageIndex = e.NewPageIndex;
bindGrid ( );
}
void bindGrid ( ) {
myGrid.DataSource = fetchView ( );
myGrid.DataBind ( );
lblTracker.Text = "Page " + ( myGrid.CurrentPageIndex+1 ) + " of " +
myGrid.PageCount;
}
DataView fetchView ( ) {
// fetch data segment
DataTable dataSegment = fetchData ( query, "gear" ).Tables [ 0 ];
// store top and end key values
DataRow [ ] currentRows = dataSegment.Select ( "","ProductID" );
ViewState [ "topID" ] = currentRows [ 0 ] [ "ProductID" ];
ViewState [ "endID" ] = currentRows [ currentRows.Length-1 ] [ "ProductID" ];
// return sorted DataView
DataView gridView = dataSegment.DefaultView;
gridView.Sort = "ProductID";
return gridView;
}
int countRows ( ) {
// specify the data source
OleDbConnection myConn = new OleDbConnection (
"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " +
Server.MapPath ( "~/app_data/gear.mdb" ) );
query = "SELECT Count ( * ) FROM Products where SubType='" + category + "'";
OleDbCommand getCount = new OleDbCommand ( query, myConn );
myConn.Open ( );
rowCount = ( int ) getCount.ExecuteScalar ( );
myConn.Close ( );
return rowCount;
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Custom Paging Using Primary Key Values</h2></div>
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<table width="92%">
<tr>
<td>Select Gear: <asp:dropdownlist id="typesList"
datatextfield="Type" onSelectedIndexChanged="getSubTypes"
autopostback runat="server" /></td>
<td>Category: <asp:dropdownlist id="subtypesList"
datatextfield="SubType" onSelectedIndexChanged="getFirstPage"
autopostback runat="server" /></td>
<td align="right" width=30%><b><asp:label id="lblTracker" runat="server" /></td></tr>
</table>
<asp:datagrid id="myGrid" runat="server"
width="92%" cellpadding=5
font-size="9pt"
gridlines="horizontal"
showheader=false
itemstyle-verticalalign="top"
autogeneratecolumns=false
allowpaging
allowcustompaging
pagesize=4
onPageIndexChanged="setPage">
<pagerstyle
position="topandbottom"
nextpagetext="Next" prevpagetext="Prev"
backcolor="lightsteelblue"
font-bold
horizontalalign="right" />
<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"
itemstyle-forecolor="darkslategray"
itemstyle-font-size="10pt"
itemstyle-font-bold />
<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>
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>