Pagination with DataList ASP.NET

DataList are most versatile Control in ASP.NET but they limit some Features which we really need.

As we know, DataList is the most versatile Data Bound Control available in ASP.NET. Where you can display data as per your need in your required style, direction etc. 
But there is some problem also. There is no option to Paginate the Data Loaded in DataList directly. In case we have too many records, we can not display all them in One Page without paginating. 
To escape from this situation, Developers move to GridView, which has easy support for Paginating data.
Today, we will learn How to Paginate DataList data in Simple steps.
First create a DataList as per your requirement.
Now place Next and Previous Buttons for Navigation.  
Sample Code for design is below:
CODE in ASPX Page
 
  1. <div>  
  2. <asp:DataList ID="DataList1" runat="server" Height="194px" Width="174px"   
  3. oneditcommand="DataList1_EditCommand"   
  4. oncancelcommand="DataList1_CancelCommand"   
  5. onupdatecommand="DataList1_UpdateCommand" DataKeyField="eno">  
  6. <HeaderTemplate>  
  7. <table width="450" border="2">  
  8. <tr>  
  9. <th>Employee Name </th>  
  10. <th>Designation </th>  
  11. <th>Salary </th>  
  12. <th>  
  13. Edit  
  14. </th>  
  15. </tr>  
  16. </HeaderTemplate>   
  17. <EditItemTemplate>  
  18. <tr>  
  19. <td>  
  20. <asp:TextBox ID="txtEname" Text='<%#Eval("ename")%>' runat="server" />  
  21. </td>  
  22. <td>  
  23. <asp:TextBox ID="txtJOb" Text='<%#Eval("job")%>' runat="server" />  
  24. </td>  
  25. <td>  
  26. <asp:TextBox ID="txtSal" Text='<%#Eval("sal")%>' runat="server" />  
  27. </td>  
  28. <td>  
  29. <asp:LinkButton ID="lnk2" runat="server" Text="Update" CommandName ="Update" />  
  30. </td>  
  31. <td>  
  32. <asp:LinkButton ID="LinkButton1" runat="server" Text="Cancel"  
  33. CommandName ="Cancel" />  
  34. </td>  
  35. </tr>  
  36. </EditItemTemplate>   
  37. <ItemTemplate>  
  38. <tr>  
  39. <td><%#Eval("ename")%></td>  
  40. <td><%#Eval("job")%></td>  
  41. <td><%#Eval("sal")%></td>  
  42. <td>  
  43. <asp:LinkButton ID="lnk1" CommandName="Edit"  Text="Edit !" runat="server" />  
  44. </td>  
  45. </tr>  
  46. </ItemTemplate>   
  47. <FooterTemplate>  
  48. </table>  
  49. </FooterTemplate>   
  50. </asp:DataList>  
  51. <br />  
  52. </div>  
  53. <table class="style1">  
  54. <tr>  
  55. <td class="style2">  
  56. <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Next</asp:LinkButton>  
  57. </td>  
  58. <td>  
  59. <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">Previous</asp:LinkButton>  
  60. </td>  
  61. </tr>  
  62. </table> 
 
 
Now Lets to to Backend. ASPX.CS Page (Code Behind):
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (!IsPostBack)  
  4. {  
  5. CurrentPageIndex = 0;  
  6. showData();   
  7. }  
  8. }  
  9. int pg = 0;  
  10. void showData()  
  11. {  
  12. PagedDataSource pgd = new PagedDataSource();  
  13. SqlDataAdapter da = new SqlDataAdapter("select * from emp""Data Source=.\\SQLDB;Database=Test1;Integrated Security=True");  
  14. DataSet ds = new DataSet();  
  15. da.Fill(ds);  
  16. pgd.DataSource = ds.Tables[0].DefaultView;  
  17. pgd.CurrentPageIndex = CurrentPageIndex ;  
  18. pgd.AllowPaging = true;  
  19. pgd.PageSize = 5;  
  20. LinkButton2.Enabled = !(pgd.IsLastPage);  
  21. LinkButton3.Enabled = !(pgd.IsFirstPage);  
  22. DataList1.DataSource = pgd;  
  23. DataList1.DataBind();  
  24. }  
  25. protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)  
  26. {  
  27. DataList1.EditItemIndex = e.Item.ItemIndex;  
  28. showData();   
  29. }  
  30. protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)  
  31. {  
  32. DataList1.EditItemIndex = -1;  
  33. showData();   
  34. }  
  35. protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)  
  36. {  
  37. }  
  38. public int CurrentPageIndex  
  39. {  
  40. get  
  41. {  
  42. if (ViewState["pg"] == null)  
  43. return 0;  
  44. else  
  45. return Convert.ToInt16(ViewState["pg"]);   
  46. }  
  47. set  
  48. {  
  49. ViewState["pg"] = value;   
  50. }  
  51. }   
  52. protected void LinkButton2_Click(object sender, EventArgs e)    {  
  53. CurrentPageIndex++;  
  54. showData();  
  55. }  
  56. protected void LinkButton3_Click(object sender, EventArgs e)  
  57. {  
  58. CurrentPageIndex--;  
  59. showData();  

 
Now Let me Describe things happened in above code:
Move to showData() function.
We created a new object of PagedDataSource. Paged data source as name stands clear, paginated data.
Now we wrote Query, Passed in Adapter, Filled DataSet which should be all clear, because it was Basic Task in ADO.NET. Now we gave it CurrentPageIndex. means where is page now. 
For this, just look below to a class CurrentPageIndex which is returning value of CurentPageIndex =0 if page is beling Loaded. To check whether page is loaded or postback, we take help of ViewState. If ViewState["pg"] == null, On Page load this will be null because it is just being initiated. On Postback it will pass the Int value stored in it to CurrentPageIndex.
We set Pagination in PagedDataSource true and Define Page size of 5 records per page.
Now Passed PagedDataSource object as Datasource of Datalist.
Now Linkbutton click events, We increased and Decreased value of CurrentPageIndex.
Hope this must be helpful to you. In case of Any problem, correction in code and help, feel free to comment below.