This article is in continuation with the previous article on implementing Custom Paging for ASP.NET DataGrid using the Cache object.
In case you have missed out the previous article, please check Part I
Just to recall, we declared a DataGrid with AllowPaging="true" and set the PagerStyle-Visible="false". Also, we declared a Panel with LinkButtons for First, Previous, Next and Last links for Paging and set a Command event for the link buttons "NavigationLink_Click()".
We will examine the code for implementing paging.
Global Variables used for Paging
private int TotalPages;
private int TotalRecords;
private int CurrentPage;
private int PageSize;
Code for binding the Data to the DataGrid
private void BindData()
{
SqlConnection objCon = new SqlConnection("your connection string goes here");
SqlCommand objCmd = new SqlCommand("select * from customers", objCon);
DataSet dsNew = new DataSet();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = objCmd;
da.Fill(ds);
Cache["AllRecords"] = ds;
dsNew = (DataSet)Cache["AllRecords"];
ViewState["TotalRecords"] = dsNew.Tables[0].Rows.Count;
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataSource = dsNew;
DataGrid1.DataBind();
TotalRecords = (int)ViewState["TotalRecords"];
ViewState["TotalPages"] = DataGrid1.PageCount;
ViewState["CurrentPage"] = 0;
PreviousPage.Enabled = false;
NextPage.Enabled = true;
FirstPage.Enabled = false;
LastPage.Enabled = true;
if(TotalRecords == 0)
{
DataGrid1.Visible = false;
Panel1.Visible = false;
}
else if(TotalRecords <= DataGrid1.PageSize) { DataGrid1.Visible = true; Panel1.Visible = true; lblPageDetails.Text = "1 - "+ TotalRecords.ToString() + " of " + TotalRecords.ToString(); NextPage.Enabled = false; LastPage.Enabled = false; } else { DataGrid1.Visible = true; Panel1.Visible = true; lblPageDetails.Text = "1 - "+ DataGrid1.PageSize.ToString() + " of " + TotalRecords.ToString(); } }
Code for the Pager link "NavigationLink_Click()"
protected void NavigationLink_Click (Object sender, CommandEventArgs e)
{
int RecordCount;
DataSet dSet = null;
dSet = (DataSet)Cache["AllRecords"];
PageSize = DataGrid1.PageSize;
switch ( e.CommandName )
{
case "First":
ViewState["CurrentPage"] = 0;
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataSource = dSet;
DataGrid1.DataBind();
PreviousPage.Enabled = false;
NextPage.Enabled = true;
FirstPage.Enabled = false;
LastPage.Enabled = true;
TotalRecords = (int)ViewState["TotalRecords"];
lblPageDetails.Text = "1 - "+ PageSize.ToString() + " of " + TotalRecords.ToString();
break;
case "Last":
TotalPages = (int) ViewState["TotalPages"] ;
ViewState["CurrentPage"] = TotalPages - 1;
DataGrid1.CurrentPageIndex = TotalPages - 1 ; //since page number starts from 0
DataGrid1.DataSource = dSet;
DataGrid1.DataBind();
NextPage.Enabled = false;
PreviousPage.Enabled = true;
LastPage.Enabled = false;
FirstPage.Enabled = true;
RecordCount = (TotalPages - 1)* PageSize;
TotalRecords = (int)ViewState["TotalRecords"];
lblPageDetails.Text = (RecordCount+1).ToString()+" - "+ TotalRecords.ToString() + " of " + TotalRecords.ToString();
break;
case "Next":
CurrentPage =(int) ViewState["CurrentPage"];
CurrentPage = CurrentPage + 1;
ViewState["CurrentPage"] = CurrentPage;
DataGrid1.CurrentPageIndex = CurrentPage;
DataGrid1.DataSource = dSet;
DataGrid1.DataBind();
RecordCount = CurrentPage * PageSize;
TotalRecords = (int)ViewState["TotalRecords"];
if(CurrentPage == DataGrid1.PageCount-1)
{
NextPage.Enabled = false;
PreviousPage.Enabled = true;
LastPage.Enabled = false;
FirstPage.Enabled = true;
lblPageDetails.Text = (RecordCount+1).ToString()+" - "+ TotalRecords.ToString() + " of " + TotalRecords.ToString();
}
else
{
NextPage.Enabled = true;
PreviousPage.Enabled = true;
LastPage.Enabled = true;
FirstPage.Enabled = true;
lblPageDetails.Text = (RecordCount+1).ToString()+" - "+ (RecordCount+PageSize).ToString() + " of " + TotalRecords.ToString();
}
break;
case "Prev":
CurrentPage =(int) ViewState["CurrentPage"];
CurrentPage = CurrentPage - 1;
ViewState["CurrentPage"] = CurrentPage;
DataGrid1.CurrentPageIndex = CurrentPage;
DataGrid1.DataSource = dSet;
DataGrid1.DataBind();
RecordCount = CurrentPage * PageSize;
TotalRecords = (int)ViewState["TotalRecords"];
if(CurrentPage == 0)
{
PreviousPage.Enabled = false;
NextPage.Enabled = true;
FirstPage.Enabled = false;
LastPage.Enabled = true;
lblPageDetails.Text = "1 - "+ PageSize.ToString() + " of " +TotalRecords.ToString();
}
else
{
NextPage.Enabled = true;
PreviousPage.Enabled = true;
LastPage.Enabled = true;
FirstPage.Enabled = true;
lblPageDetails.Text = (RecordCount+1).ToString()+" - "+ (RecordCount+PageSize).ToString() + " of " + TotalRecords.ToString();
}
break;
}
}
Calling the DataGrid Binding method in the Page_Load Event
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
We will examine the code step-by-step:-
1. Declaring Global variables
We have declared certain global variables such that they are useful across the methods.
2. Binding the Data to the DataGrid
The first seven lines of code must be a familiar one for whoever creates a connection object, dataadapter and a dataset. We have used a sample "Customer" Table for this sample.
What is changed is that, we dont directly bind the DataGrid with the DataSet. Instead, we store the DataSet into the Cache object. The reason is that, the Cache object is going to store all the records that we retrieve from the Database in one shot and then remain useful in moving next,previous link buttons in the paging.
We then typecast the ViewState data into a new DataSet which is used to bind the DataGrid.
We then store the total number of records into ViewState. This is useful in showing the total records in the Pager Panel.
Then, we assign the current page index to 0 so that the DataGrid's first page is shown when the control generates for the first time.
The DataGrid is then bound to the New DataSet.
Then, the TotalPages the DataGrid requies to show all the records is set to ViewState and also the CurrentPageIndex is set to ViewState.
We then do various checkings to show, enable and disable the pager links based on the number of records etc., which is self-explanatory.
3. The Navigation_Link Method Code
The first thing we do in this method is to get the items from the Cache. Remember, this event is going to fire for any of the pager links clicked.
We then do a switch based on the CommandName argument that is passed and do relevant calculations to show respective count on the Pager links.
4. Page_Load Event
In the Page_Load event, the DataGrid binding method is called within the PostBack Condition check.
As you are aware, there are various calculations done based on the Link clicked such that it shows, the relevant items in the format "1-5 of 10", "6-10 of 10" etc., and also there is a First and Last link which can take you to the first page and last page of the records.
This also improves the performance since the bulk of records is stored in the Cache object and thereafter, there is no Database call unlike the traditional built-in paging functionality. This also accounts to quicker response.
You can also notice that the format of the Pager (text) is now in our control we can configure the Text as per our requirement unlike the limited options in the built-in paging mechanism.
This concludes the 2 part article series on implementing Custom Paging using the Cache object for ASP.NET DataGrid.
Cheers and Happy Programming !!!
posted @ Wednesday, December 28, 2005 1:47 PM