Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have list for add_to_cart product , now i want to add pagination to it,
like first 1,2 Last, can i add pagination to this

following is my code

List<Product> prod = Session["cart"] as List<Product>; // typeCast session into List
        int totalProd = prod.Count;
        //prod = prod.Skip(2 * 1).Take(2).ToList();
        
        products_gw.DataSource = prod;
        products_gw.DataBind();


and also when i remove item from then pagination get removed

following is remove item function

Button btn = (Button)sender;
        int id = Convert.ToInt32(btn.CommandArgument);
        int pageIndex = 1;
        List<Product> prod = Session["cart"] as List<Product>;
        prod.RemoveAll(r => r.prodID == id);
        int totalProd = prod.Count;
        products_gw.DataSource = prod;
        products_gw.DataBind();


What I have tried:

i try following code

List<Product> prod = Session["cart"] as List<Product>; // typeCast session into List
        int totalProd = prod.Count;
int recordCount = Convert.ToInt32(totalProd);
        this.PopulatePager(recordCount, pageIndex);

<pre> private void PopulatePager(int recordCount, int currentPage)
    {
        try
        {
            double dblPageCount = (double)((decimal)recordCount / 2);
            int pageCount = (int)Math.Ceiling(dblPageCount);
            List<ListItem> pages = new List<ListItem>();
            
            if (pageCount > 0)
            {
                pages.Add(new ListItem("First", "1", currentPage > 1));
                for (int i = 1; i <= pageCount; i++)
                {
                    pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
                    
                }
                pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
            }
            rptPager.DataSource = pages;

            
            rptPager.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }
Posted
Updated 15-Dec-19 19:57pm
Comments
Mohibur Rashid 10-Dec-19 2:25am    
have you tried google?
https://jasonwatmore.com/post/2018/10/17/c-pure-pagination-logic-in-c-aspnet
This is the first link
Akshay malvankar 10-Dec-19 19:25pm    
how this can be applied to list<t>, that is my question,how to do it, as above i try one code but it doesnt work

 
Share this answer
 
hello try this method for pagination
C#
static IList<int> GetPage(IList<int> list, int pageNumber, int pageSize = 10)
{
    return list.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900