Export (0) Print
Expand All

TemplateControl Class

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

For a list of all members of this type, see TemplateControl Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.TemplateControl
         System.Web.UI.Page
         System.Web.UI.UserControl

[Visual Basic]
MustInherit Public Class TemplateControl
   Inherits Control
   Implements INamingContainer
[C#]
public abstract class TemplateControl : Control, INamingContainer
[C++]
public __gc __abstract class TemplateControl : public Control,
   INamingContainer
[JScript]
public abstract class TemplateControl extends Control implements
   INamingContainer

Thread Safety

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

Example

[Visual Basic] 
' Create a code-behind custom UserControl class (the UserControl class 
' is derived from TemplateControl), named MyControl, that
' overrides the Construct method of the TemplateControl class.
' When the control is initialized, the overridden Construct method
' is called, which allows the custom initialization logic
' to be called as well.
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

' The custom user control class.
 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 and
   ' name it myControl.
   Public Event myControl As System.EventHandler
   
   
   ' Override the default constructor.
   Protected Overrides Sub Construct()
      ' Specify the handler, MyInit, to be called when the myControl event
      ' is raised by the OnInit method.
      AddHandler Me.myControl, AddressOf MyInit
   End Sub 'Construct
   
   
   Protected Overrides Sub OnInit(e As EventArgs)
      RaiseEvent myControl(Me, e)
      Response.Write("The OnInit() method is used to raise the Init event.")
   End Sub 'OnInit
   
   
   ' Use the MyInit handler to set the Message property value
   ' when this method is called.
   Sub MyInit(sender As Object, e As System.EventArgs)
      _message = "Hello World!"
   End Sub 'MyInit
   
   ' Write the value of the Message property when the control
   ' is rendered.
   Protected Overrides Sub Render(output As HtmlTextWriter)
      output.Write(("<br>Message :" & _message))
   End Sub 'Render
 End Class 'MyControl

[C#] 
// Create a code-behind custom UserControl class (the UserControl class 
// is derived from TemplateControl), named MyControl, that
// overrides the Construct method of the TemplateControl class.
// When the control is initialized, the overridden Construct method
// is called, which allows the custom initialization logic
// to be called as well.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;



 // The custom user control class.
 public class MyControl:UserControl
 {
   // Create a Message property and accessors.
   private string _message = null;
   public string Message
   {
      get
      {
         return _message;
      }
      set
      {
         _message = value;
      }
   }

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

   // Override the default constructor.
   protected override void Construct()
   {
      // Specify the handler, MyInit, to be called when the myControl event
      // is raised by the OnInit method.
      this.myControl += new System.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 value
   // when this method is called.
   void MyInit(object sender,System.EventArgs e)
   {
      _message = "Hello World!";
   }

   // Write the value of the Message property when the control
   // is rendered.
   protected override void Render(HtmlTextWriter output) 
   {
      output.Write("<br>Message :" + _message);
   }
 }

[C++] 
// Create a code-behind custom UserControl class (the UserControl class 
// is derived from TemplateControl), named MyControl, that
// overrides the Construct method of the TemplateControl class.
// When the control is initialized, the overridden Construct method
// is called, which allows the custom initialization logic
// to be called as well.
#using <mscorlib.dll>
#using <System.Web.dll>
#using <System.dll>
using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;



// The custom user control class.
public __gc class MyControl:public UserControl
{
   // Create a Message property and accessors.
private:
   String* _message;
public:
   __property String* get_Message()
   {
      return _message;
   }
   __property void set_Message( String* value )
   {
      _message = value;
   }

   // Create an event for this user control and
   // name it myControl.
public:
   __event System::EventHandler* myControl;

   // Override the default constructor.
protected:
   void Construct()
   {
      // Specify the handler, MyInit, to be called when the myControl event
      // is raised by the OnInit method.
      this->myControl += new System::EventHandler(this, &MyControl::MyInit);    
   }

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

   // Use the MyInit handler to set the Message property value
   // when this method is called.
   void MyInit(Object* sender,System::EventArgs* e)
   {
      _message = S"Hello World!";
   }

   // Write the value of the Message property when the control
   // is rendered.
   void Render(HtmlTextWriter* output) 
   {
      output->Write(S"<br>Message :{0}", _message);
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

TemplateControl Members | System.Web.UI Namespace

Show:
© 2014 Microsoft