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

asp.net控件开发基础(4)--[转载]

2008-07-18 18:31 447 查看
我们根据属性的不同表现形式,把其区分为简单属性和复杂属性

下面来看下属性的表现形式

简单属性表现形式如下,大家都很熟悉

<asp:TextBox ID="TextBox1" Text="textbox控件" runat="server"></asp:TextBox>

属性中含有子属性,称之为复杂对象,如Font属性

复杂属性的表现形式如下,

(1)连字符的表现形式

<asp:TextBox ID="TextBox1" Text="textbox控件" runat="server" Font-Bold="True"></asp:TextBox>

(2)内镶属性的表现形式,如定义样式

<asp:DataList ID="DataList1" runat="server">

<SelectedItemStyle />

<EditItemStyle />

</asp:DataList>

(3)内镶集合属性的表现形式,如DropDownList (先不介绍,大家可看MSDN)
<asp:DropDownList ID="DropDownList1" runat="server">

<asp:ListItem>x</asp:ListItem>

<asp:ListItem>xx</asp:ListItem>

<asp:ListItem>xxx</asp:ListItem>

</asp:DropDownList>

下面得好好看

1,复杂属性基本使用方法



请看我是怎么做的,关于下面看到了一些元数据,如果你不熟悉,请参考MSDN.

下面一段代码记录一个custom的信息.

1.1 定义枚举


using System;

namespace CustomComponents

using System;

using System.ComponentModel;

namespace CustomComponents

using System;

using System.ComponentModel;

using System.Web;

using System.Web.UI;

namespace CustomComponents

1.4 在asp.net页面定义控件,

发现问题:
属性不是有效属性,如下图

[Description("地址集合")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

public Address CustomAddress

[/b][TypeConverter(typeof(ExpandableObjectConverter))]

public class Address

[NotifyParentProperty(true)]

public String Street

[/b] <custom:custom id="Custom1" runat="server" name="Clingingboy" CustomMetier="教师" Age="21">

<CustomAddress City="杭州" Street="不告诉你" State="中国" Zip="310000" />

</custom:custom>

发现问题:无法使用内镶属性

1.8 实现内镶属性

解决方法:在Custom类中给CustomAddress再加入一个元数据(第三个),如下

[Description("地址集合")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

[PersistenceMode(PersistenceMode.InnerProperty)]

public Address CustomAddress

<custom:custom

CustomAddress-Zip="3100001" CustomAddress-City="杭州1"

CustomAddress-State="中国1" CustomAddress-Street="不告诉你1"

id="Custom1" runat="server" name="Clingingboy" CustomMetier="教师" Age="21">

<CustomAddress City="杭州" Street="不告诉你" State="中国" Zip="310000" />

</custom:custom>

发现问题:查看属性面板,再次修改CustomAddress子属性,然后运行,发现修改后无效果,而且显示的数据仍然是连字符属性的数据(非内镶属性的)

1.9 让控件支持内镶属性

解决方法:
给Custom类添加元数据,如下代码

[ParseChildren(true)]

public class Custom: Control

[ParseChildren(true)]

[PersistChildren(false)]

public class Custom: Control

{ }

编译后再次修改属性面板的值,发现修改的是内镶属性的数据,而且这次修改后数据没有丢失,运行后也是修改后的效果.

好了,简单的讲完了.

总结下:上面刚开始到1.3为止,其实效果已经实现了,接下来都是添加元数据,添加以后给我们带来的是方便.以上解决问题的办法全是套用元数据.可能上面的元数据大家很熟悉,在MSDN里面字面解释的也很清楚,但你去试验过吗?我相信这样的试验可以让你明白的更加深刻.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: