asp.net下用AspNetPager分頁控件對DataList進行分頁


第一次寫博客,其實注冊挺久的了,一直不知道寫些什么好,原因是自己菜鳥一個,知識淺陋,不好出來獻丑。

但是慢慢接觸的多了,總覺得有些東西是不是該寫一寫,跟分享一下!不敢說對大家都有用,互相學習,也權當是給自己學習的路上留下一點印記吧!

廢話不說了,進入主題:

最近在做一個兼顧前后台的項目,用到了很多都是用DataList綁定數據和用AspNetPager實現對數據進行分頁管理的知識

先來看aspx文件代碼

<div class="orderslist">
                   <!--增加datalist-->       
              <table id="customers" width="100%" cellpadding="0" cellspacing="0" border="1px">
            	    <tbody>
                    <tr align="center" valign="middle" height="30px">
                      <th width="30%">訂單編號</th>
                      <th width="50%">訂餐時間</th>
                      <th width="20%">操作</th>
                    </tr>
                   <asp:DataList ID = "Orders_list" runat = "server" RepeatLayout = "Flow" RepeatDirection="Horizontal">
                        <ItemTemplate>
                            <tr height="30px" align="center" valign="middle">
                                  <td><%# Eval("ord_no")%></td>
                                  <td><%# Eval("ord_time")%></td>
                                  <td><a href="OrdersDetail.aspx?ord_no=<%# Eval("ord_no")%>">查看</a> </td> 	
                            </tr>
                        </ItemTemplate> 
                     </asp:DataList>
                    </tbody>
                </table>
             </div>

           <div class="fenye">
                      <%-- 分頁控件--%>
                         <center>
                            <webdiyer:AspNetPager ID="AspNetPagerNotice" runat="server" CurrentPageButtonClass="cpb"
                                   Width="570px" ShowPageIndexBox="Always" PageSize="16"
                                   FirstPageText="首頁" LastPageText="尾頁" PrevPageText="上頁" NextPageText="下頁"
                                   OnPageChanged="AspNetPagerNotice_PageChanged"
                                   CurrentPageButtonStyle="color:#f60" NumericButtonCount="6" Font-Bold="False" 
                                   Font-Names="微軟雅黑">
                           </webdiyer:AspNetPager>
                      </center>
           </div>

  再來看看cs文件代碼:

public partial class OrdersManage : System.Web.UI.Page
{
    /// <summary>
    /// 單頁面中信息數量
    /// </summary>
    private int Orders_limit = 16;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Refresh();
        }
    }

    public void Refresh()
    {
        using (BLLS o = new BLLS())
        {
            /*統計訂單數量*/
            string strsql = "select count(*) from orders";
            DataSet myds = o.Select(strsql);
            AspNetPagerNotice.RecordCount = Convert.ToInt32(myds.Tables[0].Rows[0][0].ToString());

            /*取前n條數據*/
            string strsql1 = "select top(@P0) * from orders order by ord_id";
            this.Orders_list.DataSource = o.Select(strsql1, Orders_limit);
            this.Orders_list.DataBind();//DataList綁定數據源
        }
    }



    protected void AspNetPagerNotice_PageChanged(object sender, EventArgs e)
    {
        using (BLLS category = new BLLS())
        {
            string strsql2 = "select top (@p0) * From orders where ord_id Not in ( select top (@p1) ord_id From orders Order By ord_id) Order By ord_id";
            Orders_limit = AspNetPagerNotice.PageSize;//單頁面訂單顯示數目
            int start = AspNetPagerNotice.StartRecordIndex - 1;
            this.Orders_list.DataSource = category.Select(strsql2, Orders_limit, start);
            this.Orders_list.DataBind();
        }
    }

}

好了,有了上面的代碼,看一下效果圖:

 

好了,基本上是大功告成!!

但是如果要去挑一些毛病的話,還是存在一些問題的:

我想在下頁前面始終顯示最后一頁(第20頁)的按鈕,我也查看了屬性卻不知道怎么實現,網上倒是有一些自己寫方法實現的資料;

還請大神指點一二!!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM