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

asp.net控件开发基础(14)

2006-09-29 21:05 471 查看
上一篇讨论了为服务器控件添加客户端功能,这一篇我们所要讲的是控件生成器

1.错误的代码,无法解析

首先来看一段简单的代码

正确
<asp:Wizard ID="Wizard1" runat="server">
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
21212</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2">
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
错误
<asp:Wizard ID="Wizard2" runat="server">
<asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
21212</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2">
</asp:WizardStep>
</asp:Wizard>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</asp:Label>
<br />
<asp:TextBox ID="TextBox3" runat="server">12345</asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Label">12345</asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server">
<asp:Label runat="server" Text="Label"></asp:Label>
</asp:TextBox>

Wizard为asp.net2.0新增的一个控件,这个页面发生两个错误,如下图

[ParseChildren(true)]
public class Custom: Control
[ParseChildren(true, "DropItemList")]
public class DropColor:WebControl
[ParseChildren(true, "Items")]
public class ItemRotator : CompositeControl

public class RItem : Control
<custom:ItemRotator
id="ItemRotator1"
Runat="server">
<custom:ritem ID="Item1" runat="server">
First Item
</custom:ritem>
<custom:ritem ID="Item2" runat="server">
Second Item
<asp:Calendar
id="Calendar1"
Runat="server" />
</custom:ritem>
<custom:ritem ID="Item3" runat="server">
Third Item
</custom:ritem>
</custom:ItemRotator>

效果就不说了,随机显示ritem控件的内容,注意以上控件定义了一个Items集合属性

另外改进的话就是我们第十篇的讲的,为Ritem定义属性,作为一个集合属性,这里就不再列出代码.

(1)ParseChildren(false)的使用

此控件未添加属性,而多了一个方法AddParsedSubObject()

控件有默认的页面分析逻辑,重写AddParsedSubObject方法,可以向控件添加子控件


示例二


[ParseChildren(false)]
public class ContentRotator : WebControl
[
ToolboxItem(false)
]
public class Content : Control
<custom:ContentRotator
id="ContentRotator1"
Runat="server">
<custom:Content
id="Content1"
Runat="server">
显示的第一项,此不为属性
</custom:Content>
<custom:Content
id="Content2"
Runat="server">
显示的第二项,此不为属性
<asp:Calendar
id="Calendar1"
Runat="server" />
</custom:Content>
<custom:Content
id="Content3"
Runat="server">
显示的第三项,此不为属性
</custom:Content>
</custom:ContentRotator>

注意:ContentRotator无任何属性(其内部添加的为控件),而是用AddParsedSubObject 方法向控件添加了子控件,而不像ItemRotator控件一样,其内部是属性而非控件.

4.修改默认解析逻辑

上面已经说过每个控件都有默认的解析逻辑,其通过ControlBuilder 类来实现,可以通过重写其方法来自定义解析逻辑.下面通过一个例子来说明,它把一个控件以自定义标签所代替

以下列出部分代码

示例三

//自定义页分析器
public class ServerTabsBuilder : ControlBuilder

[ToolboxItem(false)]
public class ServerTab : Control
protected override void AddParsedSubObject(object obj)
[ControlBuilder(typeof(ServerTabsBuilder))]
[ParseChildren(false)]
public class ServerTabs : WebControl, IPostBackEventHandler
<custom:ServerTabs
ID="ServerTabs1"
Runat="Server">
<tab Text="First Tab">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Contents of the first tab
</tab>
<tab Text="Second Tab">
Contents of the second tab
</tab>
<tab Text="Third Tab">
Contents of the third tab
</tab>
</custom:ServerTabs>

以上镶套代码为tab标签,而非<custom:ServerTabs></custom:ServerTabs>,但实现效果是一样的,只是我们改了默认的页分析逻辑,自定义了控件页生成器(分析器)
看下效果(当重新编译后需要重新启动vs2005才能看到效果)



好了,这次的主题也讲完了,这里需要注意的是asp.net2.0中复合控件只需要继承CompositeControl类即可.

这里预祝大家国庆快乐.

示例代码下载

下一篇将对此系列写下来的十四篇文章进行一个小结,并进行补充,当然小结并非是结束,接着我们还会继续讨论模板以及更多.希望大家喜欢.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: