ControlDesigner中,DesignTimeHtml的Get流程
先来个简单的Control
显然,这个Control什么都做不了。写它的目的是为了追踪System.Web.UI.Design.ControlDesigner.GetDesignTimeHtml()究竟做了什么事。
编译->放到工具箱->启动另一个IDE->创建一个Web应用程序->回到控件编辑的IDE->启动进程监视->设置断点(断点位置见代码)->回到Web应用程序IDE->把SimpleControl扔到Webform1里
然后你发现了什么?对,没错,DesignTimeHtml的Get流程:
首先,IDE检查SimpleControlDesigner的GetDesignTimeHtml(),
然后根据SimpleControlDesigner.GetDesignTimeHtml()指示,调用base.GetDesignTimeHtml(),也就是System.Web.UI.Design.ControlDesigner的GetDesignTimeHtml()
那么ControlDesigner的GetDesignTimeHtml()做了什么呢?原来它和运行时刻一样(其实还是有一些区别的,我以后再详细解释),搞了一个HtmlTextWriter,请求SimpleControl.Render()生成Html,以便在IDE里显示。
至此,我们可以发现,除了自己一行行的拼凑外,也可以让控件本身的Render()来生成DesignTimeHtml。
抛弃GetDesignTimeHtml()
现在来做个不需要实现GetDesignTimeHtml()方法,或者连ControlDesigner都不用,却能提供丰富灵活的DesignTime呈现的WebControl
下面是SimpleControl的设计时及运行时的效果,
设计时:
图中SimpleControl的属性设置为
<cc1:SimpleControl
id="SimpleControl2"
runat="server"
style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 32px"
Text="我的SimpleControl"
Font-Size="10.5pt"
Width="120px"
Height="128px"
BackColor="Linen"
BorderWidth="1px"
Font-Bold="True"
>
</cc1:SimpleControl>
id="SimpleControl2"
runat="server"
style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 32px"
Text="我的SimpleControl"
Font-Size="10.5pt"
Width="120px"
Height="128px"
BackColor="Linen"
BorderWidth="1px"
Font-Bold="True"
>
</cc1:SimpleControl>
运行效果:
子控件的样式处理
再来个稍复杂点的控件 CaptionText。
设计时效果:
图中控件的属性为
运行时效果
CaptionText 演示在设计模式下,对嵌套子控件的样式处理,详情参见代码中的Render及DoPreRender方法。
今天是头一次以Blog的方式写心得,再加上我原本就是个思路不太清楚的,所以表达的很乱。见谅见谅。