Objective: To learn how to use the DATAGRID to display data by PAGE
Although the page display efficiency of the DATAGRID is not very high, in any case, it is the most convenient page display, and it is quite simple and practical for making a message book. There is also an important requirement for enabling the paging function. The paging function can be enabled only when the data source of the DATADRID control implements the ICOLLECTION interface. DATAREADER does not have this interface, so it must be replaced by DATATABLE.
<Script runat = "server" language = "c #">
Void Page_Load ()
{
String strConnection = "Provider = Microsoft. Jet. OleDb.4.0; Data Source = ";
StrConnection + = Server. MapPath ("guestbook. mdb ");
OleDbConnection objConnection = new OleDbConnection (strConnection );
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter ("select * from guestbook", objConnection );
DataSet objDataSet = new DataSet ();
ObjDataAdapter. Fill (objDataSet );
DgrdMain. DataSource = objDataSet;
DgrdMain. DataBind ();
}
Void dgrdMain_PageIndexChanged (Object sender, DataGridPageChangedEventArgs e)
{
DgrdMain. CurrentPageIndex = e. NewPageIndex;
DataBind ();
}
</Script>
<Html>
<Body>
<Asp: DataGrid
Id = "dgrdMain"
Cellpadding = "1"
Showheader = "true"
Borderwidth = "0"
Allowpaging = "true" enable Paging
Pagesize = "3" each page displays three records
Onpageindexchanged = "dgrdMain_pageindexchanged" Call the function on page X to dgrdMain_PageIndexChanged ()
Runat = "server"
/>
</Body>
</Html>
<Script runat = "server" language = "vb">
SubPage_Load
Dim objConnection as OleDbConnection
Dim objDataAdapter as OleDbDataAdapter
Dim objDataSet as DataSet
ObjConnection = new OleDbConnection ("Provider = Microsoft. Jet. OleDb.4.0; Data Source =" + Server. MapPath ("guestbook. mdb "))
ObjDataAdapter = new OleDbDataAdapter ("select * from guestbook", objConnection)
ObjDataSet = new DataSet
ObjDataAdapter. Fill (objDataSet)
DgrdMain. DataSource = objDataSet
DgrdMain. DataBind ()
End sub
Sub dgrdMain_PageIndexChanged (sender as object, e as datagridpagechangedeventargs)
DgrdMain. CurrentPageIndex = e. NewPageIndex
DataBind
End sub
</Script>
<Html>
<Body>
<Asp: DataGrid
Id = "dgrdMain"
Cellpadding = "1"
Showheader = "true"
Borderwidth = "0"
Allowpaging = "true" enable Paging
Pagesize = "3" each page displays three records
Onpageindexchanged = "dgrdMain_pageindexchanged" Call the function on page X to dgrdMain_PageIndexChanged ()
Runat = "server"
/>
</Body>
</Html>
This is the simplest page. You can use the pagestyle-mode = "nextprev/numericpages" parameter to set whether to display the previous page, the next page, or directly select the number of the page to redirect the page. For the former, we can use pagestyle-nextpagetext to set the text containing the next page Link, and use pagestyle-prevpagetext to set the text containing the previous page Link; if it is the latter, we can use PagerStyle-PageButtonCount to set the number of pages displayed before the ellipsis (10 by default)
Let's talk about the SESSION and COOKIE tomorrow.
Http://blog.csdn.net/byebye8742/archive/2004/02/02/19501.aspx