ShowRepeater模拟简单的Repeater

为了更好的理解常用的Repeater等数据控件,自己就试着写一下,这里只当笔记!

ViewRepeater.ascx里没什么好说的,只有一个占位符

<% @ Control Language = " C# "  AutoEventWireup = " true "  CodeBehind = " ViewRepeater.ascx.cs "  Inherits = " ShowRepeater.TLC "   %>
< asp:PlaceHolder  ID ="PlaceHolder1"  runat ="server" ></ asp:PlaceHolder >

 

cs文件时多了一点

 

代码
public   partial   class  ShowRepeater : UserControl, INamingContainer
    {
        
private  ITemplate _itemTemplate  =   null ;
        
private  ITemplate _headerTemplate  =   null ;
        
private  ITemplate _footerTemplate  =   null ;
        
private   object  _dataSource  =   null ;

        
public   object  DataSource
        {
            
get  {  return  _dataSource; }
            
set  {
                
if  (value  is  IEnumerable)
                    _dataSource 
=  value;
                
else   if  (value  is  IListSource)
                    _dataSource 
=  ((IListSource)value).GetList();
                
else
                    
throw   new  ArgumentException( " DataSource应该是IEnumerable. " );
            }
        }
        [TemplateContainer(
typeof (ItemContainer))]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        
public  ITemplate ItemTemplate
        {
            
get
            {
                
return  _itemTemplate;
            }
            
set
            {
                _itemTemplate 
=  value;
            }
        }
        [TemplateContainer(
typeof (HeaderContainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        
public  ITemplate HeaderTemplate {
            
get  {  return  _headerTemplate; }
            
set  { _headerTemplate  =  value; }
        }
        [TemplateContainer(
typeof (HeaderContainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        
public  ITemplate FooterTemplate
        {
            
get  {  return  _footerTemplate; }
            
set  { _footerTemplate  =  value; }
        }

        
public   override   void  DataBind()
        {
            
if  (_headerTemplate  !=   null )
            {
                HeaderContainer hcon 
=   new  HeaderContainer();
                _headerTemplate.InstantiateIn(hcon);
                PlaceHolder1.Controls.Add(hcon);
            }

            
if  (_itemTemplate  !=   null   &&  _dataSource  !=   null )
            {
                
int  i  =   0 ;
                IEnumerable enu 
=  (IEnumerable)_dataSource;
                
foreach  ( object  o  in  enu)
                {
                    ItemContainer con 
=   new  ItemContainer(i, o);
                    _itemTemplate.InstantiateIn(con);
                    PlaceHolder1.Controls.Add(con);
                    i
++ ;
                }
            }

            
if  (_footerTemplate  !=   null )
            {
                FooterContainer fcon 
=   new  FooterContainer();
                _footerTemplate.InstantiateIn(fcon);
                PlaceHolder1.Controls.Add(fcon);
            }
            
base .DataBind();
        }

        
public   class  ItemContainer : Control, INamingContainer, IDataItemContainer
        {
            
private   int  _itemIndex;
            
private   object  _dataItem;
            
internal  ItemContainer( int  itemIndex, object  dataItem)
            {
                _itemIndex 
=  itemIndex;
                _dataItem 
=  dataItem;
            }
            
public   int  Index {  get  {  return  _itemIndex; } }

            
#region  IDataItemContainer 成员

            
public   object  DataItem
            {
                
get  {
                    
return  _dataItem;
                }
            }

            
public   int  DataItemIndex
            {
                
get  {  return  _itemIndex; }
            }

            
public   int  DisplayIndex
            {
                
get  {  return  _itemIndex; }
            }

            
#endregion
        }

        
public   class  HeaderContainer : Control, INamingContainer { }
        
public   class  FooterContainer : Control, INamingContainer { }

    }

 

用一个页面一测试,哈哈居然可以啦!欣慰!

前端主要代码:

代码
<% @ Register TagPrefix = " uc "  TagName = " vp "  Src = " ~/ViewRepeater.ascx "   %>

< uc:vp  runat ="server"  ID ="vp" >
        
< HeaderTemplate >
            
< table  width ="500" >
                
< tr >
                    
< th >
                        ID
                    
</ th >
                    
< th >
                        TItle
                    
</ th >
                    
< th >
                        DESC
                    
</ th >
                
</ tr >
        
</ HeaderTemplate >
        
< ItemTemplate >
            
< tr >
                
< td >
                    
<% Eval ( " ID " %>
                
</ td >
                
< td >
                    
<% Eval ( " Title " %>
                
</ td >
                
< td >
                    
<% Eval ( " Desc " %>
                
</ td >
            
</ tr >
        
</ ItemTemplate >
        
< FooterTemplate >
            
</ table >
        
</ FooterTemplate >
    
</ uc:vp >

 

后端:

 

代码
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! IsPostBack)
            {
                vp.DataSource 
=  gettable();
                vp.DataBind();
            }
        }
        DataTable gettable()
        {
            DataTable dt 
=   new  DataTable();
            dt.Columns.Add(
new  DataColumn( " ID " typeof ( int )));
            dt.Columns.Add(
new  DataColumn( " Title " typeof ( string )));
            dt.Columns.Add(
new  DataColumn ( " Desc " , typeof  ( string )));
            
for  ( int  i  =   0 ; i  <   5 ; i ++ )
            {
                DataRow dr 
=  dt.NewRow();
                dr[
0 =  i  +   1 ;
                dr[
1 =   " Title "   +  (i  +   1 );
                dr[
2 =   " Desc "   +  (i  +   1 );
                dt.Rows.Add(dr);
            }
            
return  dt;
        }

 

效果如图:

ShowRepeater模拟简单的Repeater

下载            参考

 

现在还是懵懵懂懂,继续学习中....

 

你可能感兴趣的:(show)