This documentation is archived and is not being maintained.

TemplateControl Class

Provides the Page class and the UserControl class with a base set of functionality.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

'Declaration
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public MustInherit Class TemplateControl _
	Inherits Control _
	Implements INamingContainer, IFilterResolutionService
'Usage
Dim instance As TemplateControl
<asp:TemplateControl />

The TemplateControl class is an abstract class that provides common properties and methods for the Page class and the UserControl class. A new instance of the TemplateControl is not created directly.

The TemplateControl class defines methods supporting declarative data-binding expressions:

  • Use the Eval method for data-binding expression involving data sources, such as simple properties or expressions.

  • Use the XPath method for parsing and evaluating an XPath data-binding expression.

  • Use the XPathSelect method for data-binding against an expression containing an XPath select statement. The result is a node collection that implements the IEnumerable interface.

For more information on data-binding expression, see Data-Binding Expression Syntax and Binding to Databases.

The following code example demonstrates how to derive a control named MyControl from the TemplateControl class and override the Construct method. When MyControl is initialized, the overridden Construct method is called.

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions

' The custom user control class.
<AspNetHostingPermission(SecurityAction.Demand, _
   Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class MyControl
   Inherits UserControl
   ' Create a Message property and accessors. 
   Private _message As String = Nothing 

   Public Property Message() As String 
      Get 
         Return _message
      End Get 
      Set
         _message = value
      End Set 
   End Property 

    ' Create an event for this user control 
    Public Event myControl As System.EventHandler

    ' Override the default constructor. 
    Protected Overrides Sub Construct()
        ' Specify the handler for the OnInit method. 
        AddHandler Me.myControl, AddressOf MyInit
    End Sub 

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        RaiseEvent myControl(Me, e)
        Response.Write("The OnInit() method is used to raise the Init event.")
    End Sub 


    ' Use the MyInit handler to set the Message property 
    Sub MyInit(ByVal sender As Object, ByVal e As System.EventArgs)
        Message = "Hello World!" 
    End Sub 

    ' Render the value of the Message property 
    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
        writer.Write(("<br>Message :" & Message))
    End Sub 
End Class

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: