Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

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)
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class TemplateControl : Control, 
	INamingContainer, IFilterResolutionService
<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.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;

// The custom user control class.
[AspNetHostingPermission(SecurityAction.Demand,
   Level = AspNetHostingPermissionLevel.Minimal)]
public class MyControl : UserControl
{
    // Create a Message property and accessors. 
    private string _message = "No message";

    public string Message
    {
        get { return _message; }
        set { _message = value; }
    }

    // Create an event for this user control 
    public event System.EventHandler myControl;

    // Override the default constructor. 
    protected override void Construct()
    {
        // Specify the handler for the OnInit method. 
        this.myControl += new EventHandler(MyInit);
    }

    protected override void OnInit(EventArgs e)
    {
        myControl(this, e);
        Response.Write("The OnInit() method is used to raise the Init event.");
    }

    // Use the MyInit handler to set the Message property 
    void MyInit(object sender, System.EventArgs e)
    {
        Message = "Hello World!";
    }

    // Render the value of the Message property 
    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<br>Message :" + Message);
    }
}
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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.