• 模板控件概念(转)


    模板控件概念:

    模板控件可以使内容与外观分离。典型的应用有RepeaterDataList

    ASP.NET 提供了一种称为模板的通用功能,允许将控制数据与其表示分开。模板化控件本身不提供用户界面 (UI)。该控件的 UI 由页面开发人员通过内联模板提供,该模板允许页面开发人员自定义该控件的 UI。

    开发模板化控件的方法:

    1. 实现 System.Web.UI.INamingContainer 接口。这是没有任何方法的标记接口。它可以在您的控件下创建新的命名范围,这样子控件就在名称树中有了唯一的标识符。
      [C#]
      public class TemplatedFirstControl : Control,INamingContainer {...}
    2. 将 ParseChildrenAttribute 应用到您的控件,并传递 true 作为参数。在 ASP.NET 页上声明性地使用控件时,这样可指示页分析器如何分析模板属性标记。步骤 3 说明如何定义一个模板属性。
      注意   如果您的控件是从 WebControl 派生的,则不需要应用 ParseChildrenAttribute,因为 WebControl 已经用该属性作了标记。
      [C#]
      [ ParseChildren(ChildrenAsProperties = true)]
      public class TemplatedFirstControl : Control, INamingContainer {...}
      有关 ParseChildrenAttribute 的更多信息,请参见使用 ParseChildrenAttribute。
    3. 定义 System.Web.UI.ITemplate 类型的一个或多个属性。ITemplate 有一个方法 InstantiateIn,该方法可使用页上内联提供的模板创建控件。您不必实现 InstantiateIn 方法;ASP.NET 页框架可提供这种实现。ITemplate 属性必须有 System.Web.UI.TemplateContainerAttribute 类型的元数据属性,它指出哪种 INamingContainer 控件将拥有实例化模板。这在步骤 4 中作了说明。以下代码片段定义了一个模板属性。
      [C#]
      [TemplateContainer(typeof(FirstTemplateContainer))] public ITemplate FirstTemplate {...}
    4. TemplateContainerAttribute 的参数是您想在其中实例化模板的容器控件类型。容器控件独立于正在创作的模板化控件。具有逻辑容器的原因是:模板化控件通常有一个模板,该模板需要使用不同数据重复实例化。拥有与根模板化控件不同的容器控件,使拥有多个此类示例成为可能。逻辑容器是该模板内子控件的即时 INamingContainer。在开发模板化数据绑定控件中更详细地介绍了这种关系。
      注意   容器控件本身必须实现 INamingContainer,因为它有需要在页上唯一命名的子控件。
      [C#]
      public class FirstTemplateContainer : Control, INamingContainer {...}
    5. 重写 CreateChildControls 方法以便在模板中创建子控件。这是通过三个步骤来完成的。
      1. 实例化模板容器。
      2. 调用模板属性的 InstantiateIn 方法并将该容器作为参数传递给它。InstantiateIn 方法(在 ITemplate 接口中声明)实例化该模板的元素,作为该模板容器的子控件。您不必实现 InstantiateIn 方法;ASP.NET 页框架可提供这种实现。
      3. 将模板容器的示例添加到您的模板化控件的 Controls 集合。
        以下代码片段说明了 CreateChildControls 的实现。
        [C#]
        private Control myTemplateContainer;
        protected override void CreateChildControls () {
        if (FirstTemplate != null)
        {
        myTemplateContainer = new FirstTemplateContainer(this);
        FirstTemplate.InstantiateIn(myTemplateContainer);
        Controls.Add(myTemplateContainer);
        }
        else
        {
        Controls.Add(new LiteralControl(Text + " " + DateTime));
        }
        }
    6. 重写从 Control 继承的 OnDataBinding 方法以调用 EnsureChildControls 方法。这样可保证在页框架尝试计算模板内任何数据绑定表达式之前,创建模板中的子控件。您还必须调用基类的 OnDataBinding 方法以确保调用已注册的事件处理程序。
      [C#]
      protected override void OnDataBinding(EventArgs e) {
      EnsureChildControls();
      base.OnDataBinding(e);
      }
    7. 在步骤 5 中,在 CreateChildControls 方法内重复该逻辑,以便为控件的每个模板属性实例化一个模板。

    相关知识:

    有关ParseChildrenAttribute的概念:

    ParseChildrenAttribute 是在类级别应用的 .NET Framework 元数据属性。它使控件能够指定当在 ASP.NET 页上以声明方式使用该控件时,页分析器如何解释在控件标记内嵌套的元素。可以使用与布尔标志对应的命名参数和该控件的默认属性创建 ParseChildrenAttribute。有关该属性语法的详细信息,请参见 System.Web.UI.ParseChildrenAttribute。

    以下示例说明如何将 ParseChildrenAttribute 应用于一个控件。

    [C#]

    [ ParseChildren(ChildrenAsProperties = true)]

    // Or apply as [ ParseChildren(true)].

    public class TemplatedFirstControl : Control, INamingContainer {...}

    [Visual Basic]

    <ParseChildren(ChildrenAsProperties := True)> _

    Public Class TemplatedFirstControl

       Inherits Control

       Implements INamingContainer

       ...

    End Class

    有关用 ParseChildrenAttribute 标记的控件的示例,请参见模板化控件示例。当在一页上以声明方式使用 TemplatedFirstControl 时,其控件标记内的子元素(直接子元素)必须对应于 TemplatedFirstControl 的属性,如以下示例中所示。

    <%-- FirstTemplate is a property of TemplatedFirstControl. --%>

    <%-- Custom is the tag prefix for TemplatedFirstControl on the page as defined in the <%@ Register %> directive. --%>

    <Custom:TemplatedFirstControl id = "First" Text= "The time on the server is " runat=server>                        

       <FirstTemplate>

           <h3><font face="Verdana" color = "red"><%# Container.Text %> <%# Container.DateTime %>

           </font></h3>

        </FirstTemplate>     

    </Custom:TemplatedFirstControl>

    下表描述 ParseChildrenAttribute 的使用模式。

    属性用法 说明

    ParseChildrenAttribute(ChildrenAsProperties = true)

    ParseChildren(true)

     嵌套的(子)元素必须对应于控件的属性。控件标记间的其他(非属性)元素和文本生成分析器错误。

    示例:Repeater 和其他数据绑定控件。

    ParseChildrenAttribute(ChildrenAsProperties = false)

    ParseChildrenAttribute(false)

    ParseChildrenAttribute 不应用于该控件。

     嵌套的(子)元素必须对应于 ASP.NET 服务器控件。页分析器创建子控件并对该控件调用 IParserAccessor.AddParsedSubObject。AddParsedSubObject 的默认实现将子控件添加到该控件的 Controls 集合中。页分析器将标记间的文本分析为 LiteralControl 的实例。

    示例:Panel。

    ParseChildrenAttribute (ChildrenAsProperties = true, DefaultProperty = "PropertyName")

    ParseChildrenAttribute(true, "PropertyName")

     控件必须定义公共属性名 PropertyName。嵌套的(子)元素必须对应于 PropertyName 属性的子元素。

    示例:HtmlTable、HtmlTableRow。

    有关示例,请参见 ParseChildrenAttribute 示例。

    ParseChildrenAttribute(false, "PropertyName") 页分析器的这个用法等同于 ParseChildrenAttribute(false)。

    注意   System.Web.UI.WebControls.WebControl 被标记为 ParseChildren(true)。从 WebControl 派生的自定义控件继承此属性标记。可以通过重新应用具有不同参数的属性来重写继承的属性。

    注意   如果除 ParseChildrenAttribute 之外还应用了 ControlBuilderAttribute,则它可以修改在该表中描述的分析逻辑。

  • 相关阅读:
    1.14验证码 彩票
    String代码示例
    1.13作业
    控制台输入人数和分数 自动判断最高分最低分
    对矩阵进行转置运算
    16、输入三角形的三个边,求其面积
    02、
    15、判断字符串是否回文——字符串
    14、求出最大元素的下标及地址值——数组
    13、字符串在指定的地方以及元素个数实行逆置——字符串
  • 原文地址:https://www.cnblogs.com/yuxiang9999/p/421065.html
Copyright © 2020-2023  润新知