Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have Datalist control in asp.net page, My clent asked me to enable paging on product page.I want add few line of code in my existing application and want to enable paging.

Anybody please help.

ASP.Net page code

XML
<asp:DataList ID="dlistProductDetails" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
                    <ItemTemplate>
                        <table width="185" height="230" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td width="80" valign="top">
                                    <a href='<%# Eval("url") %>'>
                                        <img src='<%#Eval("image") %>' width="160" class="gallery" /></a>
                                </td>
                            </tr>
                            <tr>
                                <td valign="middle">
                                    <span><a href='<%# Eval("url") %>' style="text-decoration: none;">
                                        <%#Eval("ProdName") %></a></span>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <%--  <span>Rs.<%#Eval("price") %></span>--%>
                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </table>

                    </ItemTemplate>
                </asp:DataList>


ASP.CS file Code
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
           CurrentPageIndex = 0;
           int cid = Convert.ToInt32(Request.QueryString["CatID"].ToString());
           int pid = Convert.ToInt32(Request.QueryString["PId"].ToString());

           Bindproduct(cid,pid);
       }
   }

   protected void Bindproduct(int cid,int pid)
   {
       if (pid == 0)
       {
           var res = (from i in dc.Product_MSTs
                      where i.ParentCatId == cid
                      orderby i.ProdId descending

                      select new
                      {
                          url = "ProductDetails.aspx?Cid=" + i.CatID + "&Pid=" + i.ProdId,
                          name = i.CateName,
                          ProdName=i.Prod_code,
                          image = "UploadImages/" + i.Image1,
                        //  price = i.Price,
                           i.ParentCatName,
                      }).ToList();




           if (res.Count > 0)
           {
               dlistProductDetails.DataSource = res;
               dlistProductDetails.DataBind();

               lblCatogaries.Text = res[0].ParentCatName;
               //lblSubcatogaries.Text = res[0].CateName;
              // lblSubcatogaries1.Text = res[0].CateName;
           }
           else
           {
               dlistProductDetails.DataSource = null;
               dlistProductDetails.DataBind();
           }
       }
       else
       {
           var res = (from i in dc.Product_MSTs
                      where i.CatID == cid && i.ParentCatId == pid
                      orderby i.ProdId descending

                      select new
                      {
                          i.ParentCatName,
                          i.CateName,
                          url = "ProductDetails.aspx?Cid=" + i.CatID + "&Pid=" + i.ProdId,
                          ProdName = i.Prod_code,
                          image = "UploadImages/" + i.Image1,
                         // price = i.Price
                      }).ToList();
           if (res.Count > 0)
           {
               dlistProductDetails.DataSource = res;
               dlistProductDetails.DataBind();

               lblCatogaries.Text=res[0].ParentCatName;
               //lblSubcatogaries.Text = res[0].CateName;
               lblSubcatogaries1.Text = res[0].CateName;
           }
           else
           {
               dlistProductDetails.DataSource = null;
               dlistProductDetails.DataBind();
           }
       }

   }
Posted
Updated 17-Apr-15 22:26pm
v4
Comments
Maciej Los 8-Apr-15 10:39am    
What's the question?

Here is an idea: Datalist paging with linq[^]
 
Share this answer
 
Link button for previous and Next


XML
<div>
      <asp:LinkButton ID="lbtnPrev" Text="Previous" runat="server" OnClick="lbtnPrev_Click"></asp:LinkButton>
       <asp:LinkButton ID="lbtnNext" Text="Next" runat="server" OnClick="lbtnNext_Click"></asp:LinkButton>
   </div>



Use Skip() and Take()of LINQ to retrieve and skip items from product.

C#
public partial class Product : System.Web.UI.Page
{
    //your database context;
     int pageSize=16;//use amount of product wants to display
     int page = 1;
//page counting

C#
 protected void Bindproduct(int cid,int pid)// passing product ID and category
    {
var res = (from i in dc.Product_MSTs
                      where i.ParentCatId == cid
                      orderby i.ProdId descending

                      select new
                      {
                      url = "ProductDetails.aspx?Cid=" + i.CatID + "&Pid=" + i.ProdId,
                          name = i.CateName,
                          ProdName=i.ProdName,
                          image = "UploadImages/" + i.Image1,
                          price = i.Price,
                          MrpPrice=i.MrpPrice,
                      }).ToList().Skip((page-1)*pageSize).Take(pageSize);// take & skip of LINQ query
           
               dlistProductDetails.DataSource = res;// binding datasource to datalist control
               dlistProductDetails.DataBind();
}

call click event of link button to retrieve data from database. and bind with page with method to Bindproduct(cid, pid); extract exact data.

C#
protected void lbtnPrev_Click(object sender, EventArgs e)
   {
       page--;
       int cid = Convert.ToInt32(Request.QueryString["CatID"].ToString());
        int pid = Convert.ToInt32(Request.QueryString["PId"].ToString());
        Bindproduct(cid, pid);

   }
   protected void lbtnNext_Click(object sender, EventArgs e)
   {
       page++;
       int cid = Convert.ToInt32(Request.QueryString["CatID"].ToString());
        int pid = Convert.ToInt32(Request.QueryString["PId"].ToString());
        Bindproduct(cid, pid);

   }
 
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