Datagridpaging. aspxCode:
<% @ Page language = "c #" EnableViewState = "true" Codebehind = "DataGridPaging. aspx. cs"
AutoEventWireup = "false" Inherits = "eMeng. Exam. DataGridPaging. DataGridPaging" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<Html>
<Head>
<Meta content = "Visual Basic 7.0" name = "CODE_LANGUAGE">
<Meta content = "JavaScript" name = "vs_defaultClientScript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetSchema">
</Head>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" runat = "server">
<Asp: datagrid id = "MyDataGrid" runat = "server" AutoGenerateColumns = "False"
HorizontalAlign = "Center" AlternatingItemStyle-BackColor = "# eeeeee"
Headerstyle-backcolor = "# aaaadd" font-size = "8pt" font-name = "verdana"
Cellspacing = "0" cellpadding = "3" gridlines = "both" borderwidth = "1"
Bordercolor = "black" onpageindexchanged = "mydatagrid_page" pagerstyle-horizontalalign = "right"
Pagerstyle-mode = "numericpages" pagesize = "5" allowpaging = "true">
<Alternatingitemstyle backcolor = "# eeeeee"> </alternatingitemstyle>
<Headerstyle backcolor = "# aaaadd" font-bold = "true" horizontalalign = "center"> <Pagerstyle horizontalalign = "right" mode = "numericpages"> </pagerstyle>
<Columns>
<Asp: boundcolumn headertext = "title" datafield = "title" headerstyle-width = "480px">
</ASP: boundcolumn>
<Asp: BoundColumn HeaderText = "publication date" DataField = "CreateDate" DataFormatString = "{0: yyyy-MM-dd hh: mm: ss}">
</Asp: BoundColumn>
</Columns>
</Asp: datagrid>
<P style = "FONT-SIZE: 9pt" align = "center">
<Asp: label id = "lblPageCount" runat = "server"> </asp: label> & nbsp;
<Asp: label id = "lblCurrentIndex" runat = "server"> </asp: label>
<Asp: linkbutton id = "btnFirst" onclick = "PagerButtonClick" runat = "server" Font-Name = "verdana"
Font-size = "8pt" ForeColor = "navy" CommandArgument = "0"> </asp: linkbutton> & nbsp;
<Asp: linkbutton id = "btnPrev" onclick = "PagerButtonClick" runat = "server" Font-Name = "verdana"
Font-size = "8pt" ForeColor = "navy" CommandArgument = "prev"> </asp: linkbutton> & nbsp;
<Asp: linkbutton id = "btnNext" onclick = "PagerButtonClick" runat = "server" Font-Name = "verdana"
Font-size = "8pt" ForeColor = "navy" CommandArgument = "next"> </asp: linkbutton> & nbsp;
<Asp: linkbutton id = "btnLast" onclick = "PagerButtonClick" runat = "server" Font-Name = "verdana"
Font-size = "8pt" ForeColor = "navy" CommandArgument = "last"> </asp: linkbutton>
</P>
</Form>
</Body>
</Html>
Datagridpaging. aspx. CSCode:
<! -- StartFragment --> using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Data. OleDb;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Namespace eMeng. Exam. DataGridPaging
{
/// <Summary>
/// Description of the summary of DataGridPaging.
/// </Summary>
Public class DataGridPaging: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. DataGrid MyDataGrid;
Protected System. Web. UI. WebControls. Label lblPageCount;
Protected System. Web. UI. WebControls. Label lblCurrentIndex;
Protected System. Web. UI. WebControls. LinkButton btnFirst;
Protected System. Web. UI. WebControls. LinkButton btnPrev;
Protected System. Web. UI. WebControls. LinkButton btnNext;
Protected System. Web. UI. WebControls. LinkButton btnLast;
Private OleDbConnection cn = new OleDbConnection ();
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Btnfirst. Text = "Homepage ";
Btnprev. Text = "Previous Page ";
Btnnext. Text = "next page ";
Btnlast. Text = "last page ";
Opendatabase ();
Bindgrid ();
}
Private void opendatabase ()
{
CN. connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("XXXX. mdb ");
CN. open ();
}
Private void showstats ()
{
Lblcurrentindex. Text = "" + (mydatagrid. currentpageindex + 1). tostring () + "page ";
Lblpagecount. Text = "Total" + mydatagrid. pagecount. tostring () + "page ";
}
Public void pagerbuttonclick (Object sender, eventargs E)
{
String arg = (LinkButton) sender). CommandArgument. ToString ();
Switch (arg)
{
Case "next ":
If (MyDataGrid. CurrentPageIndex <(MyDataGrid. PageCount-1 ))
{
MyDataGrid. CurrentPageIndex + = 1;
}
Break;
Case "prev ":
If (MyDataGrid. CurrentPageIndex> 0)
{
MyDataGrid. CurrentPageIndex-= 1;
}
Break;
Case "last ":
MyDataGrid. CurrentPageIndex = (MyDataGrid. PageCount-1 );
Break;
Default:
MyDataGrid. CurrentPageIndex = System. Convert. ToInt32 (arg );
Break;
}
BindGrid ();
ShowStats ();
}
Public void BindGrid ()
{
OleDbConnection myConnection = cn;
DataSet ds = new DataSet ();
OleDbDataAdapter adapter = new OleDbDataAdapter ("Select Title, CreateDate from Document", myConnection );
Adapter. Fill (ds, "Document ");
MyDataGrid. DataSource = ds. Tables ["Document"]. DefaultView;
MyDataGrid. DataBind ();
ShowStats ();
}
Public void MyDataGrid_Page (object sender, DataGridPageChangedEventArgs e)
{
Int startIndex;
StartIndex = MyDataGrid. CurrentPageIndex * MyDataGrid. PageSize;
MyDataGrid. CurrentPageIndex = e. NewPageIndex;
BindGrid ();
ShowStats ();
}
# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}