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

Control Class

Defines the properties, methods, and events that are shared by all ASP.NET server controls.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
'Declaration
<BindableAttribute(True)> _
<ThemeableAttribute(False)> _
PublicClassControlImplementsIComponent, IDisposable, IParserAccessor, IUrlResolutionService, _
	IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
'Usage
DiminstanceAsControl
/** @attribute BindableAttribute(true) */ 
/** @attribute ThemeableAttribute(false) */ 
public class Control implements IComponent, IDisposable, 
	IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, 
	IExpressionsAccessor
BindableAttribute(true) 
ThemeableAttribute(false) 
public class Control implements IComponent, IDisposable, 
	IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, 
	IExpressionsAccessor
Not applicable.

This is the primary class that you derive from when you develop custom ASP.NET server controls. Control does not have any user interface (UI) specific features. If you are authoring a control that does not have a UI, or combines other controls that render their own UI, derive from Control. If you are authoring a control that does have a UI, derive from WebControl or any control in the System.Web.UI.WebControls namespace that provides an appropriate starting point for your custom control.

The Control class is the base class for all ASP.NET server controls, including custom controls, user controls, and pages. ASP.NET pages are instances of the Page class, which inherits from the Control class, and that handle requests for files that have an .aspx extension.

The Control class can directly or indirectly be used as part of the user interface for your Web application, and as such should be scrutinized to make sure best practices for writing secure code and securing applications are followed. For general information on these topics, see Overview of Web Application Security Threats, Security Policy Best Practices, and Key Security Concepts. For more specific information, see Securing Standard Controls, How to: Display Safe Error Messages, How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Introduction to the Validation Controls.

TopicLocation
How to: Locate Child Controls by ID in an ASP.NET Web PageBuilding ASP .NET Web Applications
Developing Custom Data-Bound Web Server Controls for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0Authoring ASP.NET Controls
How to: Locate the Web Forms Controls on a Page by Walking the Controls CollectionBuilding ASP .NET Web Applications
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Developing Custom Data-Bound Web Server Controls for ASP.NET 1.1Authoring ASP.NET Controls
Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
How to: Locate Child Controls by ID in an ASP.NET Web PageBuilding ASP .NET Web Applications
How to: Locate the Web Forms Controls on a Page by Walking the Controls CollectionBuilding ASP .NET Web Applications
Developing Custom Data-Bound Web Server Controls for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio

The following example demonstrates a custom server control that derives from the Control class. The InnerContent class overrides the Control.Render method, checks to see if the class has any child controls on the page and determines whether the first child of the control is a literal control. If both of these conditions are met, the overridden method writes the HTML string <H2>Your Message:, the contents of the literal control, and a closing </H2> tag to the Web Forms page.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview (Visual Studio).

Option Explicit
Option Strict

Imports System
Imports System.Web
Imports System.Web.UI
Imports Microsoft.VisualBasic

Namespace SimpleControlSamples
    Public Class InnerContent
        Inherits Control
        <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="Execution")> _
        Protected Overrides Sub Render(output As HtmlTextWriter)
            
            If HasControls() And TypeOf Controls(0) Is LiteralControl Then
                output.Write("<H2>Your message : ")
                Controls(0).RenderControl(output)
                output.Write("</H2>")
            End If
        End Sub 'Render
    End Class 'InnerContent
End Namespace 'SimpleControlSamples

package SimpleControlSamples; 

import System.*;
import System.Web.*;
import System.Web.UI.*; 
   
public class InnerContent extends Control
{
    /** @attribute System.Security.Permissions.PermissionSet(
        System.Security.Permissions.SecurityAction.Demand, Name = "Execution")
     */
    protected void Render(HtmlTextWriter output)
    {
        if (HasControls() && 
            get_Controls().get_Item(0) instanceof LiteralControl) {
            output.Write("<H2>Your message: ");
            get_Controls().get_Item(0).RenderControl(output);
            output.Write("</H2>");
        }
    } //Render
} //InnerContent

import System;
import System.Web;
import System.Web.UI;
import System.Security.Permissions;

package SimpleControlSamples {

    public class InnerContent extends Control {

       protected override function Render(output : HtmlTextWriter) {
	var securityperm : SecurityPermission;
         securityperm = new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
         securityperm.Demand();
           if ( (HasControls()) && (typeof(Controls[0]) == LiteralControl) ) {
              output.Write("<H2>Your Message: " + (LiteralControl(Controls[0])).Text + "</H2>");
           }
       }
    }    
}

System.Object
  System.Web.UI.Control
     Derived Classes
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 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 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.