创作简单的 ASP.NET 服务器控件

  1. 定义一个直接或间接从 System.Web.UI.Control 派生的类。
    [C#]
        using System;
        using System.Web.UI;Basic]
        Imports System
        Imports System.Web.UI
        Public Class FirstControl
           Inherits Control
        End Class

    using 指令允许代码从名称空间引用类型,而无须使用完全限定名。因此,Control 解析为 System.Web.UI.Control

  2. 将控件包含在名称空间中。可以定义新的名称空间或者使用现有的名称空间。名称空间的名称是 Register 页指令中名称空间伪特性的值。(例如,<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>)。有关在Basic] Namespace CustomControls Public Class FirstControl Inherits Control ... End Class End Namespace
    
        
  3. 定义控件所需的属性。以下代码片段定义了一个名为 Message 的属性。属性类似于智能字段,它们拥有访问器方法。
    [C#]
        private String message = "Hello";
        //The Message property.
        public virtual String Message{
        get{
        return message;
        }
        set{
        message = value;
        }
        }
        [Visual Basic]
        Private _message As String = "Hello"
        Public Overridable Property Message() As String
        Get
        Return _message
        End Get
        Set
        _message = value
        End Set
        End Property

    有关属性的更多信息,请参见属性概述。若要定义在往返过程中维护其状态的属性,请参见维护控件中的状态

  4. 重写控件从 Control 继承的 Render 方法。此方法提供将 HTML 发送到客户端浏览器的逻辑。控件发送到客户端的 HTML 是作为字符串参数传递给 System.Web.UI.HtmlTextWriter 实例的 Write 方法的,如下面的示例所示。
    [C#]
        protected override void Render( HtmlTextWriter writer)
        {
        writer.Write("<font> "+ this.Message + "<br>" +
        "The date and time on the server: " +
        System.DateTime.Now.ToLongTimeString()
        + "</font>");
        }
        [Visual Basic]
        Protected Overrides Sub Render(writer As HtmlTextWriter)
        writer.Write(("<font> " & Me.Message & "<br>" & _
        "The time on the server is " & _
        System.DateTime.Now.ToLongTimeString() & _
        "</font>"))
        End Sub

    在该代码片段中访问的 System.DateTime 类是一个实用工具类,它可提供日期和时间信息。请注意,该类是在服务器上调用的,因此返回服务器上的时间。

    在此代码片段中,原始 HTML 仅作为字符串参数传递给 HtmlTextWriterWrite 方法。有关使用 HtmlTextWriter 方法简化 HTML 呈现以及呈现从 WebControl 派生的控件的详细信息,请参见呈现 ASP.NET 服务器控件

    为简单起见,在本例中,FirstControl 是从 Control 派生的。如果您要创作自行呈现的组件,请从 System.Web.UI.WebControls.WebControl 中派生,以便控件可以继承 UI 特定的属性。

  5. 根据需要添加运行时和设计时特性,以便为控件提供自定义元数据。运行时特性是某些控件必需的。需要运行时特性的控件示例有:公开模板的控件、执行数据绑定的控件或者需要自定义分析逻辑的控件。有关运行时特性的示例,请参见开发模板化控件。如果要在可视化设计器(如 Visual Studio .NET)中使用控件,则需要设计时属性。设计时属性不是公共语言运行库所必需的,但这些属性提供在设计时显示一个控件所必需的元数据。下面的代码片段将设计时特性应用到第 2 步中定义的 Message 属性上。
    [C#]
        [Description("A message string to display to the user")]
        public virtual String Message{...}
        [Visual Basic]
        <Description("A message string to display to the user")> _
        Public Overridable Property Message() As String
        ...
        End Property

    有关设计时特性的更多信息,请参见组件的设计时特性属性与设计时支持

  6. 通过执行以下步骤来保存、编译和部署控件。
      using System.Web.UI; using System.Web.UI.WebControls; namespace CustomControls { public class FirstControl : Control { private String message = "Hello"; public virtual String Message { get { return message; } set { message = value; } } protected override void Render( HtmlTextWriter writer) { writer.Write("<font> " + this.Message + "<br>" + "The time on the server is " + System.DateTime.Now.ToLongTimeString() + "</font>"); } } } [Visual Basic] Option Explicit Option Strict Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Namespace CustomControls Public Class FirstControl Inherits Control Private _message As String = "Hello" Public Overridable Property Message() As String Get Return _message End Get Set _message = value End Set End Property Protected Overrides Sub Render(writer As HtmlTextWriter) writer.Write(("<font> " + Me.Message + "<br>" + "The time on the server is " + System.DateTime.Now.ToLongTimeString() + "</font>")) End Sub End Class End Namespace
      
              

      在 ASP.NET 页上使用 FirstControl

      以下 ASP.NET 页使用上一例中创建的自定义控件。Register 页指令允许页开发人员给名称空间创建别名,并为 ASP.NET 提供包含该控件的程序集的名称。本例为 CustomControls 名称空间创建别名 Custom

      <%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
              <html>
              <body>
              <form  runat=server>
              Here is a custom ASP.NET server control.<br><br>
              <Custom:FirstControl Message= "This control tells time. "  runat=server/>
              <br>
              </form>
              </body>
              </html>