Custom Paging in DataList in ASP.Net

26 06 2009

This article we describe DataList Paging with Next, previous and First, Last page access functionality. A numbers list is also displayed for accessing pages faster.

 

 

Background

Few days back when I required to implement custom paging in DataList(like a google paging). I search out the Internet but did not find any perfect code. Then after some effort I wrote my own code. I decided to share it with other users. Surely this will help other developers.

For  creating the custom paging in the DataList control we need two  DataList control, one for displaying the Data from the Database to the DataList and other for handling the  Custom Paging.

After it we create three properties

  1. CurrentPage
  2. FirstPage
  3. NextPage

The definition of the CurrentPage Property is

private int CurrentPage

    {

        get

        {

            object objPage = ViewState["_CurrentPage"];

            int _CurrentPage = 0;

            if (objPage == null)

            {

                _CurrentPage = 0;

            }

            else

            {

                _CurrentPage = (int)objPage;

            }

            return _CurrentPage;

        }

        set

        {

            ViewState["_CurrentPage"] = value;

 

        }

    }

 

 

The definition for the FirstPage property is

private int FirstPage

    {

        get

        {

           int _FirstIndex = 0;

            if (ViewState["_FirstIndex"] == null)

            {

                _FirstIndex = 0;

            }

            else

            {

                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);

            }

            return _FirstIndex;

        }

        set

        {

            ViewState["_FirstIndex"] = value;

        }

    }

The definition  for the LastPage property is

private int LastPage

    {

        get

        {

            int _LastIndex = 0;

            if (ViewState["_LastIndex"] == null)

            {

                _LastIndex = 0;

 

            }

            else

            {

                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);

            }

            return _LastIndex;

        }

        set

        {

            ViewState["_LastIndex"] = value;

        }

    }

Note:- We need two Member function

Declare it before writing the function, just below the Partial class

PagedDataSource _pageData = new PagedDataSource();

  1. BindBlogDataList()
  2. handlePaging()

 

Definition for BindBlogDataList()

private void BindBlogDataList()

    {

        DataTable dt = _objBlog.BlogContent.GetBlogDetails();

 //dataListBlogDetails.DataSource = _objBlog.BlogContent.GetBlogDetails();

        //dataListBlogDetails.DataBind();

 

        _pageData.DataSource = dt.DefaultView;

        _pageData.AllowPaging = true;

        _pageData.PageSize = 10;

        _pageData.CurrentPageIndex = CurrentPage;

        ViewState["TotalPages"] = _pageData.PageCount;

 

        this.lblPageInfo.Text = “Page ” + (CurrentPage + 1) + ” of ” +         _pageData.PageCount;

        this.lbtnPrevious.Enabled = !_pageData.IsFirstPage;

        this.lbtnNext.Enabled = !_pageData.IsLastPage;

        this.lbtnFirst.Enabled = !_pageData.IsFirstPage;

        this.lbtnLast.Enabled = !_pageData.IsLastPage;

 

        ddlBlogDetails.DataSource = _pageData;

        ddlBlogDetails.DataBind();

        this. handlePaging();

 

    }

Definition for handlePaging()

private void handlePaging ()

    {

        DataTable dt = new DataTable();

        dt.Columns.Add(“PageIndex”);

        dt.Columns.Add(“PageText”);

        FirstPage = CurrentPage – 5;

        if (CurrentPage > 5)

        {

            LastPage = CurrentPage + 5;

        }

        else

        {

            LastPage = 10;

        }

        if (LastPage > Convert.ToInt32(ViewState["TotalPages"]))

        {

            LastPage = Convert.ToInt32(ViewState["TotalPages"]);

            FirstPage = LastPage – 10;

        }

        if (FirstPage < 0)

        {

            FirstPage = 0;

        }

        for (int i = FirstPage; i < LastPage; i++)

        {

            DataRow dr = dt.NewRow();

            dr[0] = i;

            dr[1] = i + 1;

            dt.Rows.Add(dr);

        }

        this. ddlPaging.DataSource = dt;

        this. ddlPaging.DataBind();

    }

 

Details of Default.aspx page

<table width=”100%”>

            <tr>

                <td align=”center”>

                    <strong>View Blogs</strong></td>

            </tr>

            <tr>

                <td align=”left”>

                    <asp:DataList runat=”server” ID=”ddlBlogDetails ” CellPadding=”2″>

                        <ItemTemplate>

                          <asp:Label ID=”lblBlogId” runat=”server” Text=’<%#Eval(“BlogContentId”)%>’  Visible=”false”></asp:Label>

                          BlogTitle:-

                        <asp:Label ID=”Label1″ runat=”server” Text=’<%#Eval(“BlogTitle”) %>’></asp:Label>

                        <br />

                        Created On:-

                            <asp:Label ID=”lblDate” runat=”server” ReadOnly=”true” Text=’<%#Eval(“CreatedOn”) %>’ ></asp:Label>

                            <br />

                        Left by:-

                        <asp:Label ID=”lblLeftBy” runat=”server” Text=’<%#Eval(“LeftByName”) %>’></asp:Label>

                        <br />

                        Description:-

                        <asp:Label ID=”lblDetails” runat=”server” Text=’<%# myfunction(Eval(“BlogDescription”)) %>’></asp:Label>

                                                 

                        </ItemTemplate>

                        <SeparatorTemplate>

                        <hr />

                        </SeparatorTemplate>

                        <AlternatingItemStyle BackColor=”Silver” />

                        <ItemStyle BackColor=”White” />

                    </asp:DataList>

                </td>

            </tr>

            <tr>

                <td>

                 <%–<asp:LinkButton runat=”server” Text=”Create New Blog”

                        onclick=”lnkBtnCreateNewBlog_Click”></asp:LinkButton>–%>  

                 </td>

            </tr>

            <tr>

                <td>

                    <table cellpadding=”0″ border=”0″>

                        <tr>

                            <td align=”right”>

                                <asp:LinkButton ID=”lbtnFirst” runat=”server” CausesValidation=”false”

                                    onclick=”lbtnFirst_Click”>First</asp:LinkButton>

                                &nbsp;</td>

                            <td align=”right”>

                                <asp:LinkButton ID=”lbtnPrevious” runat=”server” CausesValidation=”false”

                                    onclick=”lbtnPrevious_Click”>Previous</asp:LinkButton>&nbsp;&nbsp;</td>

                            <td align=”center” valign=”middle”>

                                <asp:DataList ID=”ddlPaging” runat=”server” RepeatDirection=”Horizontal”

                                    OnItemCommand=”ddlPaging_ItemCommand” onitemdatabound=”ddlPaging_ItemDataBound”

                                   >

                                    <ItemTemplate>

                                        <asp:LinkButton ID=”lnkbtnPaging” runat=”server” CommandArgument=’<%# Eval(“PageIndex”) %>’

                                            CommandName=”Paging” Text=’<%# Eval(“PageText”) %>’></asp:LinkButton>&nbsp;

                                    </ItemTemplate>

                                </asp:DataList>

                            </td>

                            <td align=”left”>

                                &nbsp;&nbsp;<asp:LinkButton ID=”lbtnNext” runat=”server”

                                    CausesValidation=”false” onclick=”lbtnNext_Click”>Next</asp:LinkButton></td>

                            <td align=”left”>

                                &nbsp;

                                <asp:LinkButton ID=”lbtnLast” runat=”server” CausesValidation=”false”

                                    onclick=”lbtnLast_Click”>Last</asp:LinkButton></td>

                        </tr>

                        <tr>

                            <td colspan=”5″ align=”center” style=”height: 30px” valign=”middle”>

                                <asp:Label ID=”lblPageInfo” runat=”server”></asp:Label>

                                </td>

                        </tr>

                    </table>

                </td>

            </tr>

        </table>

 

Details of Default.aspx.cs file

protected void ddlPaging_ItemCommand(object source, DataListCommandEventArgs e)

    {

        if (e.CommandName.Equals(“Paging”))

        {

            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());

            this.BindBlogDataList();

        }

    }

 

 

protected void ddlPaging_ItemDataBound(object sender, DataListItemEventArgs e)

    {

        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl(“lnkbtnPaging”);

        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())

        {

            lnkbtnPage.Enabled = false;

            lnkbtnPage.Style.Add(“fone-size”, “14px”);

            lnkbtnPage.Font.Bold = true;

 

        }

    }

 

//Page Load

protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            BindBlogDataList();

        }

       

    }

//First Link Button Click

protected void lbtnFirst_Click(object sender, EventArgs e)

    {

        CurrentPage = 0;

        BindBlogDataList();

    }

//Previous Link Button Click

protected void lbtnPrevious_Click(object sender, EventArgs e)

    {

        CurrentPage -= 1;

        BindBlogDataList();

 

 

    }

//Next Link Button Click

protected void lbtnNext_Click(object sender, EventArgs e)

    {

        CurrentPage += 1;

        BindBlogDataList();

    }

//Last Link Button Click

protected void lbtnLast_Click(object sender, EventArgs e)

    {

        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) – 1);

        BindBlogDataList();

 

    }

 

 

 

 

 

 

 Custom Paging In DataList

Advertisement

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.