|
|
|
|
|||||||||||||
Introduction
In my previous article, we discussed how to
Sort data in a Datalist. Today, we will learn how we can add the
paging feature for the datalist. It should be noted that, like DataGrid, DataList does not
support inbuilt paging mechanism. The essence of this article is how we make use of
the object SqlDataAdapter. This object has a method called, Fill which is
used to add or refresh the rows in a DataSet. Actually the method Fill is overloaded.
We will be mainly concentrating the one which takes four arguments. The four arguments are
DataSet, startRecord, maxRecords and the TableName. Second and third arguments are integer.
Where as the TableName is the table name. So, if we say objDV.Fill(objDS, 0, 5, "sales")
The dataset will be filled with 5 records and the starting position will be from the first
record. First, we are bringing the entire records from the table sales, and then we filter
those with the help of startRecord and maxRecords.
Things that we will be learning in this article.
Populating the DataListFor our example, we will take the table SALES in the PUBS Database. Since stored procedures are very much better than inline query, we use a stored procedure called, sp_das_sales_sel, which contains a single SQL statement. The SQL statement would be Select * from pubs.dbo.sales. And finally, we need to bind the DataView to the DataList web server control. How to build the User Interface for Paging in a DataList?Apart from the DataList web server control, we will provide user with four hyperlinks for navigation. When the user clicks on any othe four navigation links, we will invoke a server side method which will pulls out the proper records. We will also show the total number of records, total pages and the current page number. Code for Navigation Links.
How it works?
Important Note:
The name of the file that I have used in the href property is "datalistpaging.aspx". You
should replace this with your aspx filename, unless you keep the filename as myself.
ShowFirst and the DataBind method.
How it works?
In the ShowFirst method, we are setting the value of intCurrIndex.Text to be zero.
intCurrIndex is a hidden label control which keeps track of the current record number. We
also do have two more hidden label controls. They are intPageSize and intRecordCount.
An alternate solution for the Fill method!The major disadvantage of our logic is that, if we have 1000 records in the table, we are bringing all those to our ASPX pages. All we need is the records for our current page. This can be achived by modifying our stored procedure. We should have an identity column in our table. Then, we should pass the starting position and the number of records to be retrieved to the stored procedure. By this way, we will just be bringing in the needed records, which will decrease the network traffic and throughput time. Sample output of our scenario
Download the code
Click here to download the ASPX page LinksConclusionSo, that is it. We have a datalist with paging mechanism. Also read my other article which explains adding the sort feature to a datalist. Send your comments to das@aspalliance.com Back to Article list | |||||||||||||||
| Copyright © 2000-2003 ASPAlliance.com Page Rendered at
11/21/2009 6:17:54 PM |
|||||||||||||||