petshop4学习_重构DataList实现分页

掌握要点: <IList>,DataGrid翻页事件...
详细代码如下,慢慢体会..
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Collections.Specialized;
None.gif
using  System.Text;
None.gif
using  System.Text.RegularExpressions;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
None.gif
namespace  CustomControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// CustomList 的摘要说明
InBlock.gif    
/// 重构DataList控件
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CustomList : DataList
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//静态常量
InBlock.gif
        protected const string HTML1 = "<table width=100% cellpadding=0 cellspacing=0><tbody><tr><td>";
InBlock.gif        
protected const string HTML2 = "</td></tr><tr><td align=right>";
InBlock.gif        
//protected const string HTML3 = "</td><td align=center class=paging>";
InBlock.gif
InBlock.gif        
//protected const string HTML5 = "</td><td align=right class=paging>";
InBlock.gif

InBlock.gif        
protected const string HTML4 = "</td></tr></tbody></table>";
InBlock.gif
InBlock.gif        
private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
InBlock.gif        
private const string LINK_PREV = "<a href=?page={0}>前一页</a>";
InBlock.gif        
private const string LINK_MORE = "<a href=?page={0}>下一页</a>";
InBlock.gif        
private const string LINK_FIRST = "<a href=?page={0}>首页</a>";     //首页
InBlock.gif
        private const string LINK_LAST = "<a href=?page={0}>尾页</a>";      //尾页
InBlock.gif
        private const string KEY_PAGE = "page";
InBlock.gif        
private const string COMMA = "?";
InBlock.gif        
private const string AMP = "&";
InBlock.gif
InBlock.gif        
protected string emptyText;
InBlock.gif        
private IList dataSource;       //数据源
InBlock.gif
        private int pageSize = 10;      //每页大小
InBlock.gif
        private int currentPageIndex;   //当前页
InBlock.gif
        private int itemCount;          //数据条目总数
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 指定要绑定的数据源
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        override public object DataSource
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//This try catch block is to avoid issues with the VS.NET designer
InBlock.gif                
//The designer will try and bind a datasource which does not derive from ILIST
InBlock.gif
                try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dataSource 
= (IList)value;
InBlock.gif                    ItemCount 
= dataSource.Count;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dataSource 
= null;
InBlock.gif                    ItemCount 
= 0;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 每页大小
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int PageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn pageSize; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ pageSize = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 总页数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected int PageCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (ItemCount - 1/ pageSize; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 可重载,数据条目总数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        virtual protected int ItemCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn itemCount; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ itemCount = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 可重载,当前页码
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        virtual public int CurrentPageIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn currentPageIndex; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ currentPageIndex = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 空Text
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string EmptyText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ emptyText = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置页面,翻页事件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="index"></param>

InBlock.gif        public void SetPage(int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnPageIndexChanged(
new DataGridPageChangedEventArgs(null, index));
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Overriden method to control how the page is rendered
InBlock.gif        
/// Render提交写入
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="writer"></param>

InBlock.gif        override protected void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
//Check there is some data attached
InBlock.gif
            if (ItemCount == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(emptyText);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//Mask the query "?"/"&"二个符号
InBlock.gif
            string query = Context.Request.Url.Query.Replace(COMMA, AMP);
InBlock.gif            
//正则表达式替换
InBlock.gif
            query = RX.Replace(query, string.Empty);
InBlock.gif
InBlock.gif
InBlock.gif            
// Write out the first part of the control, the table header
InBlock.gif
            writer.Write(HTML1);
InBlock.gif
InBlock.gif            
// Call the inherited method
InBlock.gif
            base.Render(writer);
InBlock.gif
InBlock.gif            
// Write out a table row closure
InBlock.gif
            writer.Write(HTML2);
InBlock.gif
InBlock.gif            
if (currentPageIndex > 0)
InBlock.gif            writer.Write(
string.Format(LINK_FIRST, 0 + query) + "&nbsp;");     //首页
InBlock.gif            
//Determin whether next and previous buttons are required
InBlock.gif            
//Previous button?
InBlock.gif
            if (currentPageIndex > 0)
InBlock.gif                writer.Write(
string.Format(LINK_PREV, (currentPageIndex - 1+ query));
InBlock.gif
InBlock.gif            
//Close the table data tag
InBlock.gif            
//writer.Write(HTML3);
InBlock.gif
InBlock.gif            
//当前页/总页数
InBlock.gif
            writer.Write("&nbsp;&nbsp;" + string.Format("页码:{0}/{1}", currentPageIndex + 1, PageCount + 1+ "&nbsp;&nbsp;");
InBlock.gif
InBlock.gif            
//writer.Write(HTML5);
InBlock.gif
InBlock.gif            
//Next button?
InBlock.gif
            if (currentPageIndex < PageCount)
InBlock.gif                writer.Write(
string.Format(LINK_MORE, (currentPageIndex + 1+ query));
InBlock.gif            
if (currentPageIndex < PageCount)
InBlock.gif            writer.Write(
"&nbsp;" + string.Format(LINK_LAST, PageCount + query));      //尾页
InBlock.gif
InBlock.gif            
//Close the table
InBlock.gif
            writer.Write(HTML4);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
override protected void OnDataBinding(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
//Work out which items we want to render to the page
InBlock.gif
            int start = CurrentPageIndex * pageSize;
InBlock.gif            
int size = Math.Min(pageSize, ItemCount - start);
InBlock.gif
InBlock.gif            IList page 
= new ArrayList();
InBlock.gif
InBlock.gif            
//Add the relevant items from the datasource
InBlock.gif
            for (int i = 0; i < size; i++)
InBlock.gif                page.Add(dataSource[start 
+ i]);
InBlock.gif
InBlock.gif            
//set the base objects datasource
InBlock.gif
            base.DataSource = page;
InBlock.gif            
base.OnDataBinding(e);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 翻页事件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event DataGridPageChangedEventHandler PageIndexChanged;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 可重载,翻页事件处理
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (PageIndexChanged != null)
InBlock.gif                PageIndexChanged(
this, e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/sjett/archive/2006/06/20/430834.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值