如何使用LINQ在ASP.NET的DataList控件中启用分页 [英] How to Enable paging in DataList control of ASP.NET using LINQ

查看:113
本文介绍了如何使用LINQ在ASP.NET的DataList控件中启用分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net页面中有Datalist控件,My clent让我在产品页面上启用分页。我想在现有应用程序中添加几行代码,并希望启用分页。



有人请帮忙。



ASP.Net页面代码



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

<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文件代码



ASP.CS file Code

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();
           }
       }

   }

推荐答案

这是一个想法:使用linq进行Datalist分页 [ ^ ]
Here is an idea: Datalist paging with linq[^]


上一页和下一页的链接按钮





Link button for previous and Next


<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>







使用LINQ的Skip()和Take()来检索和跳过项目来自产品。






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

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

 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();
}



调用点击事件链接按钮从数据库中检索数据。并使用方法将页面绑定到 Bindproduct(cid,pid); 提取确切数据。




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

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);

   }


这篇关于如何使用LINQ在ASP.NET的DataList控件中启用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆