Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 12613 |  Welcome, Guest!   Register  Login
 Home > Blogs > ASP.NET > Pagination with DataList ...
Johnbhatt

Pagination with DataList

 Blog author: Johnbhatt | Posted on: 7/15/2012 | Category: ASP.NET Blogs | Views: 793 | Status: [Member] | Points: 75 | Alert Moderator   
Ads

Hi,

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. I made like below which is quite simple.

Lets place Next and Previous Buttons for Navigation.  Code for above design is below:

Code in ASPX Page (Design)
<div>
    
        <asp:DataList ID="DataList1" runat="server" Height="194px" Width="174px" 
            oneditcommand="DataList1_EditCommand" 
            oncancelcommand="DataList1_CancelCommand" 
            onupdatecommand="DataList1_UpdateCommand" DataKeyField="eno">
            <HeaderTemplate>
              
              <table width="450" border="2">
                <tr>
                    <th>Employee Name </th>
                    <th>Designation </th>
                    <th>Salary </th>
                    <th>
                        Edit
                    </th>
                </tr>
               
            </HeaderTemplate> 
            <EditItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="txtEname" Text='<%#Eval("ename")%>' runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="txtJOb" Text='<%#Eval("job")%>' runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="txtSal" Text='<%#Eval("sal")%>' runat="server" />
                    </td>
                    <td>
                        <asp:LinkButton ID="lnk2" runat="server" Text="Update" CommandName ="Update" />
                    </td>
                    <td>
                        <asp:LinkButton ID="LinkButton1" runat="server" Text="Cancel"
                         CommandName ="Cancel" />
                    </td>


                </tr>
            
            </EditItemTemplate> 

            <ItemTemplate>
                <tr>
                    <td><%#Eval("ename")%></td>
                    <td><%#Eval("job")%></td>
                    <td><%#Eval("sal")%></td>
                    <td>
                        <asp:LinkButton ID="lnk1" CommandName="Edit"  Text="Edit !" runat="server" />
                    </td>
                </tr>
                
            </ItemTemplate> 
            
            <FooterTemplate>
              </table>
            </FooterTemplate> 
        </asp:DataList>
    
        <br />
    
    </div>
    <table class="style1">
        <tr>
            <td class="style2">
                <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Next</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">Previous</asp:LinkButton>
            </td>
        </tr>
    </table>
Now Lets to to Backend. ASPX.CS Page (Code Behind):

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CurrentPageIndex = 0;
            showData(); 
        }
    }
    int pg = 0;
    void showData()
    {
        PagedDataSource pgd = new PagedDataSource();
        SqlDataAdapter da = new SqlDataAdapter("select * from emp", "Data Source=.\\SQLDB;Database=Test1;Integrated Security=True");
        DataSet ds = new DataSet();
        da.Fill(ds);

        pgd.DataSource = ds.Tables[0].DefaultView;
        pgd.CurrentPageIndex = CurrentPageIndex ;
        pgd.AllowPaging = true;
        pgd.PageSize = 5;

        LinkButton2.Enabled = !(pgd.IsLastPage);
        LinkButton3.Enabled = !(pgd.IsFirstPage);
  
 
        DataList1.DataSource = pgd;
        DataList1.DataBind();

    }

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        showData(); 
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        showData(); 
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {

    }

    public int CurrentPageIndex
    {
        get
        {
            if (ViewState["pg"] == null)
                return 0;
            else
                return Convert.ToInt16(ViewState["pg"]); 
        }
        set
        {
            ViewState["pg"] = value; 
        }
    } 

protected void LinkButton2_Click(object sender, EventArgs e)    {
        CurrentPageIndex++;
        showData();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        CurrentPageIndex--;
        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.


 




John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com
Found interesting? Add this to:


About John Bhatt

Experience:4 year(s)
Home page:http://www.johnbhatt.com
Member since:Thursday, June 21, 2012
Level:Starter
Status: [Member]
Biography:John Bhatt is an IT Professional having interest in Web technology. He is Web Designer, Developer, Software Developer, Blogger and Technology Geek. Currently he writes his Blogs at Blog of P.Yar.B and various other Sites. He is Main author and founder of Download Center. Contact Him at : Facebook | Twitter | Website.
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 4:36:34 PM