This documentation is archived and is not being maintained.

WebControl Class

Serves as the base class that defines the methods, properties and events common to all controls in the System.Web.UI.WebControls namespace.

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

         Derived classes

[Visual Basic]
Public Class WebControl
   Inherits Control
   Implements IAttributeAccessor
public class WebControl : Control, IAttributeAccessor
public __gc class WebControl : public Control, IAttributeAccessor
public class WebControl extends Control implements

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.


The WebControl class provides the properties, methods, and events that are common to all Web server controls. You can control the appearance and behavior of a Web server control by setting properties defined in this class. For example, the background color and font color of a control are controlled by using the BackColor and ForeColor properties, respectively. On controls that can display a border, you can control the border width, the border style, and the border color by setting the BorderWidth, BorderStyle, and BorderColor properties. The size of a Web server control can be specified by using the Height and Width properties.

The behavior of the control can be specified by setting certain properties. You can enable and disable a control by setting the Enabled property. The place of the control in the tab order is controlled by setting the TabIndex property. You can specify a ToolTip for the control by setting the ToolTip property.

Note   Not all controls support every property defined in this class. For specific information about whether a property is supported, see the documentation for the specific control.
Note   Some properties in this class render differently, depending on the browser. Some properties do not render at all, while others render, but have no effect. The HttpBrowserCapabilities.TagWriter property of the System.Web.HttpBrowserCapabilities object determines the way in which a Web server control renders. For browsers that support HTML 4.0, the HttpBrowserCapabilities.TagWriter property will contain a regular System.Web.UI.HtmlTextWriter object, and most properties will be rendered using HTML 4.0 style attributes. Browsers that are not known to support HTML 4.0 will use the System.Web.UI.Html32TextWriter object. This will automatically map the style attributes to any relevant HTML 3.2 tag attributes. In some cases, such as with the ForeColor property, the style attributes will be converted into additional tags, such as <font> tags. In some cases, there will be no mapping performed. For specific information about how a property is rendered in different browsers, see the documentation for the specific property.

For a list of initial property values for an instance of WebControl, see the WebControl constructor.


[Visual Basic] 

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ControlTest
   ' Renders the following HTML: 
   ' <span onclick="alert('Hello');" style="color:Red;">Custom Contents</span>
   Public Class MyWebControl
      Inherits WebControl
      Public Sub New()
      End Sub 'New      
      <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
      Protected Overrides Sub AddAttributesToRender(writer As HtmlTextWriter)
         writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');")
         writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red")

      End Sub 'AddAttributesToRender       

      <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
      Protected Overrides Sub RenderContents(writer As HtmlTextWriter)
         writer.Write("Custom Contents")
      End Sub 'RenderContents

   End Class 'MyWebControl

End Namespace 'ControlTest

namespace ControlTest 
   using System;
   using System.Web.UI;
   using System.Web.UI.WebControls;

   // Renders the following HTML: 
   // <span onclick="alert('Hello');" style="color:Red;">Custom Contents</span>

   public class MyWebControl: WebControl {

      public MyWebControl() : base(HtmlTextWriterTag.Span) 
      { }

      [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
      protected override void AddAttributesToRender(HtmlTextWriter writer) 

         writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');");
         writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");


      [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
      protected override void RenderContents(HtmlTextWriter writer) 
         writer.Write("Custom Contents");

#using <mscorlib.dll>
#using <System.Web.dll>
#using <System.dll>
using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;

// Renders the following HTML:
// <span onclick=S"alert('Hello');" style=S"color:Red;">Custom Contents</span>

public __gc class MyWebControl : public WebControl 
   MyWebControl() : WebControl(HtmlTextWriterTag::Span) { }


   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   void AddAttributesToRender(HtmlTextWriter* writer) 
      writer->AddAttribute(HtmlTextWriterAttribute::Onclick, S"alert('Hello');");
      writer->AddStyleAttribute(HtmlTextWriterStyle::Color, S"Red");


   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   void RenderContents(HtmlTextWriter* writer) 
      writer->Write(S"Custom Contents");

[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.


Namespace: System.Web.UI.WebControls

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

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

See Also

WebControl Members | System.Web.UI.WebControls Namespace | BackColor | ForeColor | BorderWidth | BorderStyle | BorderColor | Height | Width | Enabled | TabIndex | ToolTip