asp.net控件开发基础(5)
2008-12-19 16:46
357 查看
上一篇简单的讲了从WebControl继承的控件(好象我讲的都是简单的,嘿嘿).本次讲的更简单,主题是是属性,只当分享经验,希望对大家有帮助
我们根据属性的不同表现形式,把其区分为简单属性和复杂属性
下面来看下属性的表现形式
简单属性表现形式如下,大家都很熟悉
<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里面字面解释的也很清楚,但你去试验过吗?我相信这样的试验可以让你明白的更加深刻.
这次讲的虽然很简单(难得我把这么简单的东西变的这么复杂),主要是学习学习方法了
但我还是希望对大家有帮助,上面如果哪里讲错了还请指出来^_^.
示例代码下载(请大家多修改):
参考文章:
ASP.NET 2.0服务器控件开发之简单属性
ASP.NET 2.0服务器控件开发之复杂属性 v
我们根据属性的不同表现形式,把其区分为简单属性和复杂属性
下面来看下属性的表现形式
简单属性表现形式如下,大家都很熟悉
<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里面字面解释的也很清楚,但你去试验过吗?我相信这样的试验可以让你明白的更加深刻.
这次讲的虽然很简单(难得我把这么简单的东西变的这么复杂),主要是学习学习方法了
但我还是希望对大家有帮助,上面如果哪里讲错了还请指出来^_^.
示例代码下载(请大家多修改):
参考文章:
ASP.NET 2.0服务器控件开发之简单属性
ASP.NET 2.0服务器控件开发之复杂属性 v
相关文章推荐
- asp.net控件开发基础(5)
- asp.net控件开发基础(1)
- asp.net控件开发基础(9)
- 【转】Asp.net控件开发学习笔记整理篇 - 控件开发基础
- asp.net控件开发基础系列
- asp.net控件开发基础(2)
- asp.net控件开发基础(4)--[转载]
- asp.net控件开发基础十一
- asp.net控件开发基础(13)
- asp.net控件开发基础十四
- asp.net控件开发基础(16) --------服务器模板控件
- asp.net控件开发基础(转)
- asp.net控件开发基础(1) -- Render、输出html-asp.net关注
- asp.net控件开发基础(6) -- 关于style的应用
- asp.net控件开发基础(4)
- asp.net控件开发基础(10)
- asp.net控件开发基础(11)
- asp.net控件开发基础(4)
- asp.net控件开发基础(6)
- asp.net控件开发基础(11)