webfrom-横贯四方-03-列表展示

1:列表展示

<%@ Page Title="" Language="C#" MasterPageFile="~/Master/MainMaster.Master" AutoEventWireup="true" CodeBehind="BookList.aspx.cs" Inherits="BookShopManager.Web.Goods.BookList" EnableViewState="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Repeater ID="reBookList" runat="server">
        <ItemTemplate>
            <table>
                <tbody>
                    <tr>
                        <td rowspan="2"><a
                            href="<%#Eval("Id","/BookDetail_{0}.aspx") %>">
                            <img
                                id="ctl00_cphContent_dl_Books_ctl01_imgBook"
                                style="cursor: hand" height="121"
                                alt="<%#Eval("Title") %>" hspace="4"
                                src="<%#Eval("ISBN","/Images/BookCovers/{0}.jpg") %>" width="95"></a>
                        </td>
                        <td style="font-size: small; color: red" width="650"><a
                            class="booktitle" id="link_prd_name"
                            href="<%#Eval("Id","/BookDetail_{0}.aspx") %>" target="_blank"
                            name="link_prd_name"><%#Eval("Title") %></a>
                        </td>
                    </tr>
                    <tr>
                        <td align="left"><span
                            style="font-size: 12px; line-height: 20px"><%#Eval("Author") %></span><br>
                            <br>
                            <span
                                style="font-size: 12px; line-height: 20px"><%#this.CutString(Eval("ContentDescription").ToString(),150)%></span>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" colspan="2"><span
                            style="font-weight: bold; font-size: 13px; line-height: 20px">&yen; 
                        <%#Eval("UnitPrice","{0:0.00}") %></span> </td>
                    </tr>
                </tbody>
            </table>
        </ItemTemplate>
        <SeparatorTemplate>
            <hr />
        </SeparatorTemplate>
    </asp:Repeater>
</asp:Content>
BookList.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookShopManager.Web.Goods
{
    public partial class BookList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindBookList();
            }
        }

        private void BindBookList()
        {
            BLL.Books bookBll = new BLL.Books();
            this.reBookList.DataSource = bookBll.GetModelList(string.Empty);
            this.reBookList.DataBind();
        }
        public string CutString(string str, int length) {
            return str.Length > length ? str.Substring(0, length) + "..." : str;    
        }
    }
}
BookList.aspx.cs

 2:分页功能

数据层DAL中添加方法

using Maticsoft.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BookShopManager.DAL
{
    public partial class Books
    {
        /// <summary>
        /// 获取总的记录数
        /// </summary>
        /// <returns></returns>
        public int GetRecordCount()
        {
            string str = "select count(*) from Books;";
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }
        public DataSet GetPageList(int start, int end)
        {
            string str = @"select * from (select * , ROW_NUMBER() OVER (Order by Id) as num from Books) temp
                        where temp.num between @start and @end;";
            SqlParameter[] parameters = {
                    new SqlParameter("@start", SqlDbType.start,4),
                    new SqlParameter("@end", SqlDbType.start,4)
            };
            parameters[0].Value = start;
            parameters[0].Value = end;

            return DbHelperSQL.Query(str.ToString(),parameters);
        }
    }
}
BookShopManager.DAL

业务层BLL中添加方法

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BookShopManager.BLL
{
    public partial class Books
    {
        /// <summary>
        /// 获取总页数
        /// </summary>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public int GetPageCount(int pageSize)
        {
            int recordCount = dal.GetRecordCount();
            int pageCount = Convert.ToInt32(Math.Ceiling((double)recordCount / pageSize));
            return pageCount;
        }
        public List<BookShopManager.Model.Books> GetPageList(int pageIndex, int pageSize)
        {
            int start = (pageIndex - 1) * pageSize + 1;
            int end = (pageIndex) * pageSize;
            DataSet ds = dal.GetPageList(start, end);
            return DataTableToList(ds.Tables[0]);
        }
    }
}
BookShopManager.BLL

aspx中方法

using BookShopManager.BLL;
using BookShopManager.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookShopManager.Web.Goods
{
    public partial class BookList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindBookList();
            }
        }

        private void BindBookList()
        {
            int pageIndex;
            if (!int.TryParse(Request["pageIndex"], out pageIndex))
            {
                pageIndex = 1;
            }
            BLL.Settings bllSetting = new Settings();//通过数据表配置获取pageSize
            int pageSize = ConvertHelper.ToInt(bllSetting.GetValue("pageSize"));//每页显示数目
            BLL.Books bookBll = new BLL.Books();
            int pageCount = bookBll.GetPageCount(pageSize);
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageCount = pageCount < 1 ? 1 : pageCount;
            this.reBookList.DataSource = bookBll.GetPageList(pageIndex, pageSize);
            this.reBookList.DataBind();
        }
        public string CutString(string str, int length)
        {
            return str.Length > length ? str.Substring(0, length) + "..." : str;
        }
    }
}
BookList

添加数字页码条 

using System;
using System.Collections.Generic;
using System.Text;

namespace BookShopManager.Common
{
    public partial class PageBarHelper
    {
        /// <summary>
        /// 获取底部数字页码显示条
        /// </summary>
        /// <param name="pageIndex">当前页码值</param>
        /// <param name="pageCount">总的页码值</param>
        /// <param name="pageShowCount">需要显示上下几页页码</param>
        /// <returns></returns>
        public static string GetPageBar(int pageIndex, int pageCount, int pageShowCount)
        {
            string pageBarHtml;
            //假设每次显示10页数据.pageShowCount=10,一共有18页数据
            int start = 0, end = 0;//循环开始页,循环结束页

            if (pageIndex <= pageShowCount / 2 && pageCount >= pageShowCount)
            {
                //当前页码in( 1 2 3)  ==== 1 2 3 4 5 6 7 8 9 10
                start = 1;
                end = start + pageShowCount;
            }
            else if (pageIndex <= pageShowCount / 2 && pageCount < pageShowCount)
            {
                //当前页码in( 1 2 3)  ==== 1 2 3 4 5 
                start = 1;
                end = pageCount + 1;
            }
            else if (pageIndex > pageShowCount / 2 && pageIndex + pageShowCount / 2 <= pageCount)
            {
                //当前页码in(8)  ==== 3 4 5 6 7 8 9 10 11 12 
                start = pageIndex - pageShowCount / 2;
                end = start + pageShowCount;
            }
            else if (pageIndex > pageShowCount / 2 && pageIndex + pageShowCount / 2 > pageCount)
            {
                //当前页码in(16)  ====  9 10 11 12 13 14 15 16 17 18 
                start = pageCount - pageShowCount > 0 ? pageCount - pageShowCount + 1 : 1;
                end = pageCount + 1;
            }
            StringBuilder sb = new StringBuilder();

            if (pageIndex != 1)
            {
                sb.AppendFormat(@"<a href='?pageIndex={0}'>上一页</a>", pageIndex - 1);

            }

            //循环遍历
            for (int i = start; i < end; i++)
            {


                if (i == pageIndex)
                {
                    sb.AppendFormat(@"<a href='#'>{0}</a>", i);
                }
                else
                {
                    sb.AppendFormat(@"<a href='?pageIndex={0}'>{0}</a>", i);
                }


            }
            if (pageIndex != pageCount)
            {
                sb.AppendFormat(@"<a href='?pageIndex={0}'>下一页</a>", pageIndex + 1);

            }
            pageBarHtml = sb.ToString();
            return pageBarHtml;
        }

    }

}
PageBarHelper

aspx中添加如下代码

   <link href="../Css/pageBarStyle.css" rel="stylesheet" />

<div class="page_nav">
<%=BookShopManager.Common.PageBarHelper.GetPageBar(PageIndex,PageCount,10) %>
</div>

<%@ Page Title="" Language="C#" MasterPageFile="~/Master/MainMaster.Master" AutoEventWireup="true" CodeBehind="BookList.aspx.cs" Inherits="BookShopManager.Web.Goods.BookList" EnableViewState="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
    <link href="../Css/pageBarStyle.css" rel="stylesheet" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Repeater ID="reBookList" runat="server">
        <ItemTemplate>
            <table>
                <tbody>
                    <tr>
                        <td rowspan="2"><a
                            href="<%#Eval("Id","/BookDetail_{0}.aspx") %>">
                            <img
                                id="ctl00_cphContent_dl_Books_ctl01_imgBook"
                                style="cursor: hand" height="121"
                                alt="<%#Eval("Title") %>" hspace="4"
                                src="<%#Eval("ISBN","/Images/BookCovers/{0}.jpg") %>" width="95"></a>
                        </td>
                        <td style="font-size: small; color: red" width="650"><a
                            class="booktitle" id="link_prd_name"
                            href="<%#Eval("Id","/BookDetail_{0}.aspx") %>" target="_blank"
                            name="link_prd_name"><%#Eval("Title") %></a>
                        </td>
                    </tr>
                    <tr>
                        <td align="left"><span
                            style="font-size: 12px; line-height: 20px"><%#Eval("Author") %></span><br>
                            <br>
                            <span
                                style="font-size: 12px; line-height: 20px"><%#this.CutString(Eval("ContentDescription").ToString(),150)%></span>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" colspan="2"><span
                            style="font-weight: bold; font-size: 13px; line-height: 20px">&yen; 
                        <%#Eval("UnitPrice","{0:0.00}") %></span> </td>
                    </tr>
                </tbody>
            </table>
        </ItemTemplate>
        <SeparatorTemplate>
            <hr />
        </SeparatorTemplate>
    </asp:Repeater>
    <div class="page_nav">
        <%=BookShopManager.Common.PageBarHelper.GetPageBar(PageIndex,PageCount,10) %>
    </div>
</asp:Content>
BookList.aspx
.page_nav{clear:both; padding:15px 0; color:#666; font:normal 12px/24px Arial; text-align:center;}
.page_nav a{display:inline-block; height:22px; margin:0 2px; padding:0 8px; text-decoration:none; border:solid 1px #dbe5ee; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#fff; color:#333; font:normal 12px/22px Arial, Helvetica, sans-serif; cursor:pointer;}
.page_nav strong{display:inline-block; height:24px; margin:0 3px; padding:0 8px; border:none; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#C00; color:#fff; font-weight:normal; line-height:24px; text-decoration:none;}
.page_nav a:hover,
.page_nav a.on{height:24px; margin:0 3px; border:none; background:#C00; color:#fff; line-height:24px; text-decoration:none;}
.page_nav a.select{cursor:default;}
.page_nav .view_all{display:block; text-align:center;}
.page_nav .view_all a{height:auto; margin:0; padding:0; border:none; color:#06c; line-height:24px;}
.page_nav .view_all a:hover{height:auto; margin:0; padding:0; background:none;}
pageBarStyle.css

BookList.aspx.cs中设置PageIndex和PageCount属性

using BookShopManager.BLL;
using BookShopManager.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookShopManager.Web.Goods
{
    public partial class BookList : System.Web.UI.Page
    {
        public int PageIndex { get; set; }
        public int PageCount { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindBookList();
            }
        }

        private void BindBookList()
        {
            int pageIndex;
            if (!int.TryParse(Request["pageIndex"], out pageIndex))
            {
                pageIndex = 1;
            }
            BLL.Settings bllSetting = new Settings();//通过数据表配置获取pageSize
            int pageSize = ConvertHelper.ToInt(bllSetting.GetValue("pageSize"));//每页显示数目
            BLL.Books bookBll = new BLL.Books();
            int pageCount = bookBll.GetPageCount(pageSize);
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageCount = pageCount < 1 ? 1 : pageCount;
            PageIndex = pageIndex;
            PageCount = pageCount;
            this.reBookList.DataSource = bookBll.GetPageList(pageIndex, pageSize);
            this.reBookList.DataBind();
        }
        public string CutString(string str, int length)
        {
            return str.Length > length ? str.Substring(0, length) + "..." : str;
        }
    }
}
BookList.aspx.cs

 

 

posted @ 2017-09-17 16:10  逍遥小天狼  阅读(125)  评论(0)    收藏  举报