一起谈.NET技术,asp.net控件开发基础(5)
2011-09-01 23:48
537 查看
我们根据属性的不同表现形式,把其区分为简单属性和复杂属性,下面来看下属性的表现形式。简单属性表现形式如下,大家都很熟悉
<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
[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里面字面解释的也很清楚,但你去试验过吗?我相信这样的试验可以让你明白的更加深刻。
<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
[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里面字面解释的也很清楚,但你去试验过吗?我相信这样的试验可以让你明白的更加深刻。
相关文章推荐
- 一起谈.NET技术,asp.net控件开发基础(19)
- 一起谈.NET技术,asp.net控件开发基础(12)
- 一起谈.NET技术,asp.net控件开发基础(1)
- 一起谈.NET技术,asp.net控件开发基础(11)
- 一起谈.NET技术,asp.net控件开发基础(23)
- 一起谈.NET技术,asp.net控件开发基础(10)
- 一起谈.NET技术,asp.net控件开发基础(9)
- 一起谈.NET技术,asp.net控件开发基础(8)
- 一起谈.NET技术,asp.net控件开发基础(7)
- 一起谈.NET技术,asp.net控件开发基础(17)
- 一起谈.NET技术,asp.net控件开发基础(6)
- 一起谈.NET技术,asp.net控件开发基础(22)
- 一起谈.NET技术,asp.net控件开发基础(4)
- 一起谈.NET技术,asp.net控件开发基础(15)
- 一起谈.NET技术,asp.net控件开发基础(3)
- 一起谈.NET技术,asp.net控件开发基础(18)
- 一起谈.NET技术,asp.net控件开发基础(13)
- 一起谈.NET技术,asp.net控件开发基础(14)
- 一起谈.NET技术,asp.net控件开发基础(21)
- 一起谈.NET技术,asp.net控件开发基础(2)