登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

.Net Pet Shop 4.0 Web - products  

2010-06-08 17:08:30|  分类: .net技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
products.aspx
 <%--
主要职责:
1、设置MasterPageFile
2、注册ProductControl组建
3、设置Content内容 并调用组建

--%>

<%@ Page AutoEventWireup="true" Language="C#" MasterPageFile="~/MasterPage.master" Title="Products" Inherits="PetShop.Web.Products" CodeFile="~/Products.aspx.cs" %>

<%@ Register Src="http://dszhang.blog.163.com/blog/Controls/ProductsControl.ascx" TagName="ProductsControl" TagPrefix="PetShopControl" %>

<asp:Content ID="cntPage" ContentPlaceHolderID="cphPage" runat="Server" EnableViewState="false">  
    <PetShopControl:ProductsControl ID="ProductsControl1" runat="server" />
</asp:Content>


productControl.ascx
<%--
 主要职责:
 1、初始化模板参数
 2、用户自定义数据展示(ItemTemplate)
 --%>

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductsControl.ascx.cs" Inherits="PetShop.Web.ProductsControl" EnableViewState="false" %>
<%@ Register TagPrefix="PetShopControl" Namespace="PetShop.Web" %>
<%@ OutputCache Duration="100000" VaryByParam="page;categoryId" %>

<%--
OnPageIndexChanged 通过public void SetPage(int index) 触发被调用.
--%>
<div align="center" class="productsPosition">
    <PetShopControl:CustomList ID="productsList" runat="server" EmptyText="No products found." OnPageIndexChanged="PageChanged" PageSize="4" RepeatColumns="2" NavigationNums=10 CellPadding="16" CellSpacing="0" Width="500px">       
        <ItemTemplate>
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <td valign="top" width="91"><a href='http://dszhang.blog.163.com/blog/Items.aspx?productId=<%# Eval("Id") %>&categoryId=<%# Eval("categoryId") %>'><img id="imgProduct" alt='<%# Eval("Name") %>' src='http://dszhang.blog.163.com/blog/<%# Eval("Image") %>' style="border-width: 0px;" runat="server" /></a></td>
                    <td width="26">&nbsp;</td>
                    <td valign="top" width="120"><a href='http://dszhang.blog.163.com/blog/Items.aspx?productId=<%# Eval("Id") %>&categoryId=<%# Eval("categoryId") %>'><div class="productName"><%# Eval("Name") %></div></a><div class="productDescription"><%# Eval("Description") %></div></td>                   
                </tr>              
            </table>           
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />       
    </PetShopControl:CustomList>
 </div>

CustomList.cs
//////////////////////////////////////////////////////////////////////////
// 用户List模板
// 主要功能:
// 1、处理共用参数page:
//    定义EventHandler,将NewPageIndex消息传递给被组建类,流程在OnLoad系统函数调用时,接收page参数值
// 2、覆盖Render 方法,将模板信息显示在网页上
//     分页形式显示记录条数与上下页按钮

//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PetShop.Web {

    public class CustomList : DataList {
        //Static constants
        protected const string HTML1 = "<table cellpadding=0 cellspacing=0>  ";
        protected const string HTML2 = " <tr><td class=paging align=center>";
      
        protected const string HTML3 = "</td></tr></table>";
        private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
        private const string LINK_PREV = "<a href=?page={0}>&#060;&nbsp;上一页&nbsp;</a>";
        private const string LINK_MORE = "<a href=?page={0}>下一页&nbsp;&#062;</a>";

        private const string LINK_NoPage = "<a href=?page={0}>{1}&nbsp;&nbsp;</a>";
        private const string LINK_CurPage = "<a href=?page={0}><font color=red>{1}&nbsp;&nbsp;</font></a>";

        private const string KEY_PAGE = "page";
        private const string COMMA = "?";
        private const string AMP = "&";

        protected string emptyText;
        private IList dataSource;
        private int pageSize = 10;
        private int currentPageIndex;
        private int itemCount;
        private int navigationNums = 7 ; //导航条数字

        public int NavigationNums
        {
            get { return this.navigationNums; }
            set { this.navigationNums = value; }

        }
        override public object DataSource {
            set {
                //This try catch block is to avoid issues with the VS.NET designer
                //The designer will try and bind a datasource which does not derive from ILIST
                try {
                    dataSource = (IList)value;
                    ItemCount = dataSource.Count;
                }
                catch {
                    dataSource = null;
                    ItemCount = 0;
                }
            }
        }
       
        public int PageSize {
            get { return pageSize; }
            set { pageSize = value; }
        }

        protected int PageCount {
            get { return (ItemCount - 1) / pageSize; }
        }

        //
        virtual protected int ItemCount {
            get { return itemCount; }
            set { itemCount = value; }
        }

        //用户接口方法,运行时候参数变化
        virtual public int CurrentPageIndex {
            get { return currentPageIndex; }
            set { currentPageIndex = value; }
        }

        public string EmptyText {
            set { emptyText = value; }
        }

        public void SetPage(int index) {
             OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
         }


        override protected void OnLoad(EventArgs e) {
            if (Visible) {
                string page = Context.Request[KEY_PAGE];
                int index = (page != null) ? int.Parse(page) : 0;
                SetPage(index);
            }
        }


        /// <summary>
        /// Overriden method to control how the page is rendered
        /// </summary>
        /// <param name="writer"></param>
        override protected void Render(HtmlTextWriter writer) {

            //Check there is some data attached
            if (ItemCount == 0) {
                writer.Write(emptyText);
                return;
            }

            //Mask the query
            string query = Context.Request.Url.Query.Replace(COMMA, AMP);
            query = RX.Replace(query, string.Empty);

          
            // Write out the first part of the control, the table header
            writer.Write(HTML1);

            // Write out a table row closure
            writer.Write(HTML2);

            // Call the inherited method
            base.Render(writer);

            //Determin whether next and previous buttons are required
            //Previous button?
           

            if (currentPageIndex > 1)
                writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query));
 
          
            if (currentPageIndex > PageCount)
            {
                currentPageIndex = PageCount ;

            }

            int initPage = currentPageIndex - NavigationNums / 2;
            //
            if (initPage < 1)
                initPage = 1;
            
            //数字导航
            for (int i = initPage, j = 0; i <= PageCount && j < NavigationNums; i++, j++)
            {
                if(currentPageIndex == i)
                {
                    writer.Write(string.Format(LINK_CurPage, (i) + query, i));
                }
                else
                {
                    writer.Write(string.Format(LINK_NoPage,  (i  ) + query, i ));
                }
               
            }

            //Next button?
            if (currentPageIndex   < PageCount)
                writer.Write(string.Format(LINK_MORE, (currentPageIndex +1) + query));

            //Close the table
            writer.Write(HTML3);
        }

        override protected void OnDataBinding(EventArgs e) {

            //Work out which items we want to render to the page
            if(CurrentPageIndex>PageCount)
            {
                CurrentPageIndex = PageCount -1;

            }
            //首页从1开始
            if(CurrentPageIndex <1)
            {
                CurrentPageIndex = 1;
            }
            int start = CurrentPageIndex * pageSize;
            int size = Math.Min(pageSize, ItemCount - start);

            IList page = new ArrayList();

            //Add the relevant items from the datasource
            for (int i = 0; i < size; i++)
                page.Add(dataSource[start + i]);

            //set the base objects datasource
            base.DataSource = page;
            base.OnDataBinding(e);

        }

        public event DataGridPageChangedEventHandler PageIndexChanged;

        virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e) {
            if (PageIndexChanged != null)
                PageIndexChanged(this, e);
        }
    }
}
  评论这张
 
阅读(778)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018