惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
S
Schneier on Security
aimingoo的专栏
aimingoo的专栏
T
Troy Hunt's Blog
U
Unit 42
Hacker News - Newest:
Hacker News - Newest: "LLM"
V2EX - 技术
V2EX - 技术
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
H
Heimdal Security Blog
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Cloudbric
Cloudbric
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
The Cloudflare Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
D
DataBreaches.Net
N
News and Events Feed by Topic
Security Archives - TechRepublic
Security Archives - TechRepublic
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
F
Full Disclosure
The Register - Security
The Register - Security
L
LINUX DO - 热门话题
Webroot Blog
Webroot Blog
Google Online Security Blog
Google Online Security Blog
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
T
Threatpost
Spread Privacy
Spread Privacy
小众软件
小众软件
AWS News Blog
AWS News Blog
S
Secure Thoughts
S
Security @ Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks

博客园 - jeff377

使用 Sandcastle Help File Builder 制作 VS.NET 的 HELP 文件 [ASP.NET 控件实作 Day31] TBContextMenu 控件三种不同模式的 Click 动作 [ASP.NET 控件实作 Day30] 整合 jQuery ContextMenu plugin 的右键选单控件 [ASP.NET 控件实作 Day29] 解决 DropDownList 成员 Value 值相同产生的问题 [ASP.NET 控件实作 Day28] 图形验证码控件 [ASP.NET 控件实作 Day27] 控件依 FormView CurrentMode 自行设定状态 [ASP.NET 控件实作 Day26] 让你的 GridView 与众不同 [ASP.NET 控件实作 Day25] 自订 GridView 字段 - 日期字段 [ASP.NET 控件实作 Day24] TBDropDownField 的 Items 属性的数据系结 [ASP.NET 控件实作 Day23] 自订 GridVie 字段类别 - 实作 TBDropDownField 字段类别 [ASP.NET 控件实作 Day22] 让 DropDownList 不再因项目清单不存在而造成错误 [ASP.NET 控件实作 Day21] 实作控件智能卷标 [ASP.NET 控件实作 Day20] 侦错设计阶段的程序代码 [ASP.NET 控件实作 Day18] 修改集合属性编辑器 [ASP.NET 控件实作 Day17] 集合属性包含不同型别的成员 [ASP.NET 控件实作 Day16] 继承 WebControl 实作 Toolbar 控件 [ASP.NET 控件实作 Day15] 复合控件隐藏的问题 [ASP.NET 控件实作 Day14] 继承 CompositeControl 实作 Toolbar 控件 [ASP.NET 控件实作 Day13] Flash 控件
[ASP.NET 控件实作 Day19] 控件设计阶段的外观
jeff377 · 2008-10-20 · via 博客园 - jeff377

有一些控件在执行阶段是不会呈现,也就是说控件本身在执行阶段不会 Render 出 HTML 码,例如 SqlDataSoruce、ScriptManager 这类控件;那它们在设计阶段的页面是如何呈现出来呢?本文将针对控件设计阶段的外观做进一步的说明。

程序代码下载:ASP.NET Server Control - Day19.rar

一、控件设计阶段的 HTML

Web 服务器控件的设计模式行为都是透过 ControlDesigner 来处理,连设计阶段时控件的外观也是如此;控件在设计阶段与执行执行时呈现的外观不一定相同,当然大部分会尽量一致,使其能所见即所得。

控件在设计阶段的 HTML 码是透 ControlDesigner.GetDesignTimeHtml 方法来处理,在 ControlDesigner.GetDesignTimeHtml 预设会执行控件的 RenderControl 方法,所以大部分的情况下设计阶段与执行阶段输出的 HTML 码会相同。当控件的 Visible=False 时,执行阶段是完全不会输出 HTML 码,可是在设计阶段时会特别将控件设定 Visible=True,使控件能完整呈现。

ControlDesigner.GetDesignTimeHtml 方法

Public Overridable Function GetDesignTimeHtml() As String
    Dim writer As New StringWriter(CultureInfo.InvariantCulture)
    Dim writer2 As New DesignTimeHtmlTextWriter(writer)
    Dim errorDesignTimeHtml As String = Nothing
    Dim flag As Boolean = False
    Dim visible As Boolean = True
    Dim viewControl As Control = Nothing
    Try 
        viewControl = Me.ViewControl
        visible = viewControl.Visible
        If Not visible Then
            viewControl.Visible = True
            flag = Not Me.UsePreviewControl
        End If
        viewControl.RenderControl(writer2)
        errorDesignTimeHtml = writer.ToString
    Catch exception As Exception
        errorDesignTimeHtml = Me.GetErrorDesignTimeHtml(exception)
    Finally
        If flag Then
            viewControl.Visible = visible
        End If
    End Try
    If ((Not errorDesignTimeHtml Is Nothing) AndAlso (errorDesignTimeHtml.Length <> 0)) Then
        Return errorDesignTimeHtml
    End If
    Return Me.GetEmptyDesignTimeHtml
End Function

二、自订控件的 Designer

以 TBToolbar 为例,若我们在 RenderContents 方法未针对 Items.Count=0 做输出 HTML 的处理,会发现未设定 Items 属性时,在设计页面上完全看不到 TBToolbar 控件;像这种控件设计阶段的 HTML 码,就可以自订控件的 Designer 来处理。

image

继承 ControlDesigner 命名为 TBToolbarDesigner,这个类别是用来扩充 TBToolbar 控件的设计模式行为。我们可以覆写 GetDesignTimeHtml 方法,处理设计阶段表示控件的 HTML 标记,此方法回传的 HTML 原始码就是控件呈现在设计页面的外观。所以我们可以在 TBToolbar.Items.Count=0 时,输出一段提示的 HTML 码,这样当 TBToolbar 未设定 Items 属性时一样可以在设计页面上呈现控件。

    ''' <summary>
    ''' 擴充 TBToolbar 控制項的設計模式行為。
    ''' </summary>
    Public Class TBToolbarDesigner
        Inherits System.Web.UI.Design.ControlDesigner
 
        ''' <summary>
        ''' 用來在設計階段表示控制項的 HTML 標記。
        ''' </summary>
        Public Overrides Function GetDesignTimeHtml() As String
            Dim sHTML As String
            Dim oControl As TBToolbar
 
            oControl = CType(ViewControl, TBToolbar)
            If oControl.Items.Count = 0 Then
                sHTML = "<div style=""background-color: #C0C0C0; border:solid 1px; width:200px"">請設定 Items 屬性</div>"
            Else
                sHTML = MyBase.GetDesignTimeHtml()
            End If
            Return sHTML
        End Function
 
    End Class

在 TBToolbar 控件套用 DesignerAttribute 设定自订的 TBToolbarDesigner 类别。

    <Designer(GetType(TBToolbarDesigner))> _
    Public Class TBToolbar
        Inherits WebControl
 
    End Class

重建控件组件,切换到设计页面上的看 TBToolbar 控件未设定 Items 属性时的外观,就是我们在 TBToolbarDesigner.GetDesignTimeHtml 方法回传的 HTML 码。

image

如果你觉得上述设计阶段的控件有点太阳春,我们也可以输出类似 SqlDataSource 控件的外观,将未设定 Items 属性时输出 HTML 改呼叫 CreatePlaceHolderDesignTimeHtml 方法。

            If oControl.Items.Count = 0 Then
                sHTML = MyBase.CreatePlaceHolderDesignTimeHtml("請設定 Items 屬性")
            Else
                sHTML = MyBase.GetDesignTimeHtml()
            End If

来看一下这样修改后的结果,是不是比较专业一点了呢。

image

备注:本文同步发布于「第一届iT邦帮忙铁人赛」,如果你觉得这篇文章对您有帮助,记得连上去推鉴此文增加人气 ^^
http://ithelp.ithome.com.tw/question/10012682