Listview or Datalist paging

LIstviewPagin.ASPX

 

<asp:ListView ID="_groupListView" runat="server" GroupItemCount="3">

                        <GroupTemplate>

                            <tr>

                                <asp:PlaceHolder runat="server" ID="itemPlaceholder" />

                            </tr>

                        </GroupTemplate>

                        <LayoutTemplate>

                            <table>

                                <asp:PlaceHolder ID="groupPlaceholder" runat="server" />

                            </table>

                        </LayoutTemplate>

                        <ItemTemplate>

                            <td><%#Eval("MaterialPath")%>                                      

                            </td>

                        </ItemTemplate>

                    </asp:ListView> 

 

<asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand1"

                        OnItemDataBound="dlPaging_ItemDataBound1">

                        <ItemTemplate>

                            <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'

                                CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>'></asp:LinkButton>

                        </ItemTemplate>

                    </asp:DataList>

                    <asp:LinkButton ID="LinkButton1" runat="server" Visible="false" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

                    <asp:LinkButton ID="LinkButton2" runat="server" Visible="false" OnClick="LinkButton2_Click">LinkButton</asp:LinkButton>

 

Code behind

 

         private void BindTheData()

        {

            db.CommandText = "stored procedureā€;

            db.CommandType = CommandType.StoredProcedure; 

            dt = db.ReturnDataTable(); 

            pds.DataSource = dt.DefaultView;

            pds.AllowPaging = true;

            pds.PageSize = 3;

            pds.CurrentPageIndex = CurrentPage;

            LinkButton1.Enabled = !pds.IsLastPage;

            LinkButton1.Enabled = !pds.IsFirstPage;

 

            _groupListView.DataSource = pds;

            _groupListView.DataBind();

 

            doPaging();

 

        }

         public int CurrentPage

         { 

             get

             {

                 if (this.ViewState["CurrentPage"] == null)

                     return 0;

                 else

                     return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());

             } 

             set

             {

                 this.ViewState["CurrentPage"] = value;

             } 

         }

         private void doPaging()

         {

             DataTable dt = new DataTable();

             dt.Columns.Add("PageIndex");

             dt.Columns.Add("PageText");

             for (int i = 0; i < pds.PageCount; i++)

             {

                 DataRow dr = dt.NewRow();

                 dr[0] = i;

                 dr[1] = i + 1;

                 dt.Rows.Add(dr);

             } dlPaging.DataSource = dt;

             dlPaging.DataBind();

         } 

        protected void LinkButton1_Click(object sender, EventArgs e)

        {

            CurrentPage -= 1;

            BindTheData();

        } 

        protected void LinkButton2_Click(object sender, EventArgs e)

        {

            CurrentPage += 1;

            BindTheData();

        } 

        protected void dlPaging_ItemDataBound1(object sender, DataListItemEventArgs e)

        {

            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");

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

            {

                lnkbtnPage.Enabled = false;

                lnkbtnPage.Font.Bold = true;

            }

        } 

        protected void dlPaging_ItemCommand1(object source, DataListCommandEventArgs e)

        {

            if (e.CommandName.Equals("lnkbtnPaging"))

            {

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

                BindTheData();

            }

        }