asp.net集合属性控件相关技巧

本文探讨了.NET框架中集合属性的设计与实现方法,包括定义对象集合类型、处理单集合多子类型、多集合属性控件及对象集合类型的设计。通过具体实例展示了如何提高集合属性使用的效率与灵活性。

     示例代码

让我们回头看第10篇随笔,很简单的写了下集合属性的使用,这次再深入些的讨论关于集合相关的应用


一.定义对象集合类型

None.gif          public  DropItems ItemList
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_items == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _items 
= new DropItems();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _items;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

在后台使用ItemList属性操作的时候,假设是Add操作,则里面的对象则是一个object,当然也可以,ArrayList什么都可以扔,但每一次类型的转换,明显的降低了效率.所以我们需要为其定义集合类型.如下定义

ContractedBlock.gif ExpandedBlockStart.gif
None.gif public class DropItems : CollectionBase
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif     
InBlock.gif        
private ArrayList _itemArrayList;
InBlock.gif
InBlock.gif
InBlock.gif        
public DropItems()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _itemArrayList 
= new ArrayList();
ExpandedSubBlockEnd.gif        }

InBlock.gif     
InBlock.gif
InBlock.gif        
public void Add(DropItem _item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List.Add(_item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Remove(DropItem _item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List.Remove(_item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public new int Count
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return List.Count;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DropItem this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (DropItem)_itemArrayList[index];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

然后在控件中把集合属性类型换成DropItems

None.gif          public  DropItems ItemList
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_items == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _items 
= new DropItems();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _items;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

定义集合类型的起点很多,上面代码从CollectionBase继承,你也可以继承Ilist等接口,不过这样比较麻烦,要写很多方法. .net2.0的泛型类提供了新的起点,System.Collections.ObjectModel.Collection泛型类.我们可以从这个类开始,将会节省很多的时间的.

二.单集合多子类型

相信大家熟悉asp.net2.0中的ImageMap控件,里面有一个 HotSpot集合属性,提供作用点集合,但作用点又分圆形,矩形,和多边行

 

难道我们要为控件定义三个集合类型?由于这三个作用点区域对象有很多的相似性,所以没有必要。我们只需要为其一个基类,三个子类,一个集合类就可以了.
我们来模仿着做

(1)定义button对象

ContractedBlock.gif ExpandedBlockStart.gif
None.gif    public abstract class BaseButton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string _text;
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif               
return _text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _text
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public class MyButton : BaseButton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif    
public class ImageButton : BaseButton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string _imageurl;
InBlock.gif        
public string ImageUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _imageurl;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _imageurl 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

(2)定义对象集合类型

上面说过了, 你可以从泛型类开始,可以节省很多工作,不然你也可以继承CollectionBase,注意,注释部分为继承CollectionBase的实现,从泛型类开始则一行代码也不需要写,自己选吧

ContractedBlock.gif ExpandedBlockStart.gif
None.gifpublic class ButtonCollection : Collection<BaseButton>
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
//private ArrayList _itemArrayList;
InBlock.gif        
//public ButtonCollection()
InBlock.gif        
//{
InBlock.gif        
//    _itemArrayList = new ArrayList();
InBlock.gif        
//}
InBlock.gif     
InBlock.gif
InBlock.gif        
//public void Add(BaseButton _item)
InBlock.gif        
//{
InBlock.gif        
//    List.Add(_item);
InBlock.gif        
//}
InBlock.gif
InBlock.gif        
//public void Remove(BaseButton _item)
InBlock.gif        
//{
InBlock.gif        
//    List.Remove(_item);
InBlock.gif        
//}
InBlock.gif
InBlock.gif        
//public new int Count
InBlock.gif        
//{
InBlock.gif        
//    get
InBlock.gif        
//    {
InBlock.gif        
//        return List.Count;
InBlock.gif        
//    }
InBlock.gif        
//}
InBlock.gif
InBlock.gif        
//public BaseButton this[int index]
InBlock.gif        
//{
InBlock.gif        
//    get
InBlock.gif        
//    {
InBlock.gif        
//        return (BaseButton)_itemArrayList[index];
InBlock.gif        
//    }
InBlock.gif        
//}
ExpandedBlockEnd.gif
    }

(3)定义集合属性
None.gif        [PersistenceMode(PersistenceMode.InnerDefaultProperty),
None.gif        DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
None.gif        Category(
" Behavior " )]
None.gif       
public  ButtonCollection Buttons
ExpandedBlockStart.gifContractedBlock.gif       
dot.gif {
InBlock.gif           
get
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif               
if (this._buttons == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                   
this._buttons = new ButtonCollection();
ExpandedSubBlockEnd.gif               }

InBlock.gif               
return this._buttons;
ExpandedSubBlockEnd.gif           }

ExpandedBlockEnd.gif       }

注意有些元数据特性的使用非常的重要,一定要会用.如 PersistenceMode.InnerDefaultProperty,为默认内镶属性

接着就可以在页面使用了,如下,注意到没,可以存在两种集合属性了,其实只是一种.

None.gif          < aspDemo:TestControl ID = " TestControl1 "  runat = " server "  Buttons - Capacity = " 4 " >
None.gif            
< aspDemo:MyButton Text = " aa "   />
None.gif            
< aspDemo:MyButton Text = " bbb "   />
None.gif            
< aspDemo:ImageButton ImageUrl = " image.jpg "  Text = " fasdfa "   />
None.gif            
< aspDemo:ImageButton ImageUrl = " image.jpg "  Text = " fasdfa "   />
None.gif            
< aspDemo:MyButton Text = " aa "   />
None.gif            
< aspDemo:ImageButton ImageUrl = " image.jpg "  Text = " fasdfa "   />
None.gif        
</ aspDemo:TestControl >

(4)定义编辑器

集合属性当然少不了集合编辑器了,上面的截图大家也已经看到了,如何实现呢?

还是从CollectionEditor对象继承

以前已经认识过CreateCollectionItemType方法了,这次则是CreateNewItemTypes方法,其可以返回编辑器可以编辑的类型,因为我们要编辑的对象有MyButton和ImageButton,所以如下定义

None.gif          protected   override  Type[] CreateNewItemTypes()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
return new Type[] dot.giftypeof(MyButton), typeof(ImageButton) };
ExpandedBlockEnd.gif        }

然后与集合类型关联起来,这样就算完成了.再看下效果,是不是方便多了



三.多集合属性控件

上面的以子对象作集合类型,很适合控件单集合属性的使用,如果有多个集合属性,上面这样的做法就会感觉非常的乱.看看asp.net ajax里的ScriptManager是如何做的,如下

None.gif      < asp:ScriptManager  ID ="ScriptManager1"  runat ="server" >
ExpandedBlockStart.gifContractedBlock.gif    
< Scripts > dot.gif
InBlock.gif    
<asp:ScriptReference />
InBlock.gif    
<asp:ScriptReference />
ExpandedBlockEnd.gif    
</ Scripts >
None.gif    
< Services >
None.gif    
< asp:ServiceReference  />
None.gif    
</ Services >
None.gif        
</ asp:ScriptManager >

这里真正需要将集合属性进行分类, 上面的做法不错

做法如下

(1)定义集合类型

None.gif      public   class  ImageButtonCollection : Collection < ImageButton >
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   class  MyButtonCollection : Collection < MyButton >
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedBlockEnd.gif    }

(2)作为属性使用,注意这里PersistenceMode定义为InnerProperty

None.gif  [PersistenceMode(PersistenceMode.InnerProperty),
None.gif        DesignerSerializationVisibility(
None.gif            DesignerSerializationVisibility.Content), Category(
" Behavior " )]
None.gif       
public  ImageButtonCollection Images
ExpandedBlockStart.gifContractedBlock.gif       
dot.gif {
InBlock.gif           
get
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif               
if (this._images == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                   
this._images = new ImageButtonCollection();
ExpandedSubBlockEnd.gif               }

InBlock.gif               
return this._images;
ExpandedSubBlockEnd.gif           }

ExpandedBlockEnd.gif       }

None.gif
None.gif       [PersistenceMode(PersistenceMode.InnerProperty),
None.gifDesignerSerializationVisibility(
None.gif    DesignerSerializationVisibility.Content), Category(
" Behavior " )]
None.gif       
public  MyButtonCollection MyButtons
ExpandedBlockStart.gifContractedBlock.gif       
dot.gif {
InBlock.gif           
get
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif               
if (this._mybtn == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                   
this._mybtn = new MyButtonCollection();
ExpandedSubBlockEnd.gif               }

InBlock.gif               
return this._mybtn;
ExpandedSubBlockEnd.gif           }

ExpandedBlockEnd.gif       }

现在页面标记如下,清晰很多.
None.gif          < aspDemo:TestControl  ID ="TestControl1"  runat ="server" >
None.gif        
< Buttons >
None.gif        
< aspDemo:ImageButton  />
None.gif        
< aspDemo:MyButton  />
None.gif        
</ Buttons >
None.gif            
< Images >
None.gif                
< aspDemo:ImageButton  />
None.gif            
</ Images >
None.gif            
< MyButtons >
None.gif                
< aspDemo:MyButton  />
None.gif            
</ MyButtons >
None.gif        
</ aspDemo:TestControl >

四.对象集合类型

如Table控件,先是 TableRowCollection,然后是 TableCellCollection.
再如 Tree控件,他可以无限的镶套对象,如下图




原理相同,在对象中定义集合类型

None.gif      public   class  MyButton : BaseButton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private MyButtonCollection _mybtn;
InBlock.gif
InBlock.gif        [PersistenceMode(PersistenceMode.InnerProperty),
InBlock.gifDesignerSerializationVisibility(
InBlock.gifDesignerSerializationVisibility.Content), Category(
"Behavior")]
InBlock.gif        
public MyButtonCollection MyButtons
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this._mybtn == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this._mybtn = new MyButtonCollection();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return this._mybtn;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

以上为具有集合属性控件的常用方法.说错了还请多指教.

转载于:https://www.cnblogs.com/Clingingboy/archive/2007/05/22/754921.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值