Asp.net provides three powerful list controls: DataGrid, datalist, and repeater. However, only the DataGrid Control provides the paging function. Compared with the DataGrid, datalist and repeater controls have higher style customization. Therefore, we prefer to use datalist or repeater controls to display data.
The datalist or Repeater control can be displayed on multiple pages:
1. Write a method or stored procedure and return the data table (datatable) to be displayed based on the number of incoming pages)
2. Use the pageddatasource class (located in the namespace of system. Web. UI. webcontrols)
This articleArticleThis section describes how to use the pageddatasource class to display datalist and repeater controls by page. The pageddatasource class is also used inside the DataGrid Control. The pageddatasource class encapsulates the properties of the DataGrid control. These properties enable the DataGrid to execute pagination.
Some public attributes of the pageddatasource class:
Allowcustompaging gets or sets the value indicating whether to enable custom paging.
Allowpaging gets or sets the value indicating whether to enable pagination.
Count to obtain the number of items to be used from the data source.
Currentpageindex gets or sets the index of the current page.
Datasource obtains or sets the data source.
Cececount gets the number of items in the data source.
Firstindexinpage gets the first index on the page.
Iscustompagingenabled gets a value indicating whether to enable custom paging.
Isfirstpage gets a value indicating whether the current page is a homepage.
Islastpage gets a value indicating whether the current page is the last page.
Ispagingenabled gets a value indicating whether to enable paging.
Isreadonly gets a value indicating whether the data source is read-only.
Issynchronized gets a value indicating whether to synchronize access to the data source (thread safety ).
Pagecount obtains the total number of pages required for all items in the data source.
Pagesize gets or sets the number of items to be displayed on a single page.
Virtualcount gets or sets the actual number of items in the data source when custom pages are used.
Are these attributes similar to those of the DataGrid? Yes, the DataGrid control uses the pageddatasource class to display data by page. The following example shows how to use the pageddatasource class to display datalist and repeater controls by page:
Public void page_load (Object SRC, eventargs E)
{
Oledbconnection objconn = new oledbconnection (@ "provider = Microsoft. Jet. oledb.4.0; Data Source = c: \ test. mdb ");
Oledbdataadapter objcommand = new oledbdataadapter ("select * from users", objconn );
Dataset DS = new dataset ();
Objcommand. Fill (DS );
// Assign values to relevant attributes of the pageddatasource object
Pageddatasource objpds = new pageddatasource ();
Objpds. datasource = Ds. Tables [0]. defaultview;
Objpds. allowpaging = true;
Objpps. pagesize = 5;
Int curpage;
// Obtain the query parameters from the page on the current page
If (request. querystring ["page"]! = NULL)
Curpage = convert. toint32 (request. querystring ["page"]);
Else
Curpage = 1;
Objpds. currentpageindex = CurPage-1;
Lblcurrentpage. Text = "Page:" + curpage. tostring ();
If (! Objpps. isfirstpage)
Lnkprev. navigateurl = request. currentexecutionfilepath + "? Page = "+ convert. tostring (CurPage-1 );
If (! Objpps. islastpage)
Lnknext. navigateurl = request. currentexecutionfilepath + "? Page = "+ convert. tostring (curpage + 1 );
// Assign the pageddatasource object to the Repeater control
Repeater1.datasource = objpds;
Repeater1.databind ();
}