Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Control Class
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)

Visual Basic (Declaration)
<ThemeableAttribute(False)> _
<BindableAttribute(True)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class Control _
    Implements IComponent, IDisposable, IParserAccessor, IUrlResolutionService,  _
    IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
Visual Basic (Usage)
Dim instance As Control
C#
[ThemeableAttribute(false)]
[BindableAttribute(true)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Control : IComponent, IDisposable, 
    IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, 
    IExpressionsAccessor
Visual C++
[ThemeableAttribute(false)]
[BindableAttribute(true)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class Control : IComponent, 
    IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, 
    IControlDesignerAccessor, IExpressionsAccessor
J#
/** @attribute ThemeableAttribute(false) */
/** @attribute BindableAttribute(true) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class Control implements IComponent, 
    IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, 
    IControlDesignerAccessor, IExpressionsAccessor
JScript
public class Control implements IComponent, IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor

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.

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 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 Basic
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

C#
using System;
using System.Web;
using System.Web.UI;

namespace SimpleControlSamples {

    public class InnerContent : Control {
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="Execution")] 
       protected override void Render(HtmlTextWriter output) {

           if ( (HasControls()) && (Controls[0] is LiteralControl) ) {
               output.Write("<H2>Your message : ");
               Controls[0].RenderControl(output);
               output.Write("</H2>");
           }
       }
    }    
}


J#
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

JScript
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
    System.Web.UI..::.DataBoundLiteralControl
    System.Web.UI..::.DesignerDataBoundLiteralControl
    System.Web.UI..::.DataSourceControl
    System.Web.UI..::.TemplateControl
    System.Web.UI..::.HierarchicalDataSourceControl
    System.Web.UI..::.LiteralControl
    System.Web.UI..::.BasePartialCachingControl
    System.Web.UI..::.ExtenderControl
    System.Web.UI..::.ScriptManager
    System.Web.UI..::.ScriptManagerProxy
    System.Web.UI..::.Timer
    System.Web.UI..::.UpdatePanel
    System.Web.UI..::.UpdateProgress
    System.Web.UI.WebControls..::.WebControl
    System.Web.UI.WebControls..::.View
    System.Web.UI.WebControls..::.Content
    System.Web.UI.WebControls..::.ContentPlaceHolder
    System.Web.UI.WebControls..::.HiddenField
    System.Web.UI.WebControls..::.Literal
    System.Web.UI.WebControls..::.LoginView
    System.Web.UI.WebControls..::.MenuItemTemplateContainer
    System.Web.UI.WebControls..::.MultiView
    System.Web.UI.WebControls..::.PlaceHolder
    System.Web.UI.WebControls..::.Repeater
    System.Web.UI.WebControls..::.RepeaterItem
    System.Web.UI.WebControls..::.Substitution
    System.Web.UI.WebControls..::.Xml
    System.Web.UI.WebControls..::.DataPager
    System.Web.UI.WebControls..::.DataPagerFieldItem
    System.Web.UI.WebControls..::.ListViewItem
    System.Web.UI.HtmlControls..::.HtmlControl
    System.Web.UI.WebControls.WebParts..::.ProxyWebPartManager
    System.Web.UI.WebControls.WebParts..::.WebPartManager
    System.Web.UI.MobileControls..::.MobileControl
    System.Web.UI.MobileControls..::.DeviceSpecific
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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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 SP1, 3.0, 2.0 SP1, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker