您的位置:首页 > 编程语言 > ASP

asp.net控件开发基础十

2009-03-18 00:34 543 查看
上一篇讨论了类型转换器的使用,这次继续讨论讨论集合属性的使用

集合属性相信大家都很熟悉也很常用,如DropDownList,ListBox等控件

<asp:DropDownList ID="DropDownList1" runat="server">

<asp:ListItem>测试1</asp:ListItem>

<asp:ListItem>测试2</asp:ListItem>

<asp:ListItem>测试3</asp:ListItem>

</asp:DropDownList>

1.实现集合属性效果



经过前面几篇的学习,相信这一篇看起来已经相对简单了.我们要做的就是,先定义一个复杂属性,然后用迭代语句获取数组数据即可.

如果看过前面几篇就看看下面代码吧,相信看起来很简单,我们模仿一个DropDownList,为其属性添加背景属性,代码如下

先定义一个集合属性,如下

public class DropItem

[ParseChildren(true, "DropItemList")]

public class DropColor:WebControl

<custom:DropColor ID="DropColor1" runat="server" ForeColor="White">

<custom:DropItem BackColor="Yellow" Text="黄色" Value="yellow" />

<custom:DropItem BackColor="Red" Text="红色" Value="red" />

<custom:DropItem BackColor="Blue" Text="蓝色" Value="blue" />

<custom:DropItem BackColor="Green" Text="绿色" Value="green" />

<custom:DropItem BackColor="Black" Text="黑色" Value="Black" />

</custom:DropColor>

输出以后的效果如下图

[/b] public class DropItemEditor : CollectionEditor

[

Category("Behavior"),

Description("项集合"),

DesignerSerializationVisibility(

DesignerSerializationVisibility.Content),

PersistenceMode(PersistenceMode.InnerDefaultProperty),

Editor(typeof(DropItemEditor), typeof(UITypeEditor)),

]

//定义集合属性

public ArrayList DropItemList

然后再来看下效果,这样就方面很多了.

public class DropItemConverter : ExpandableObjectConverter

[TypeConverter(typeof(DropItemConverter))]

public class DropItem

{

}

再来看下效果



好了,这回讲的比较简单又实用,希望对大家有帮助.大家同时也可以参考MSDN里的例子,下面的示例代码下载我也加上了MSDN的例子.

已经写了10篇了,我们应该有些基础了,我想大家该可以做出一些简单实用的控件了.

示例代码下载

转自:/article/4625082.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: