Export (0) Print
Expand All

Properties in ASP.NET Server Controls

.NET Framework 1.1

Properties are similar to fields but have accessor methods. You should expose properties instead of public fields from your controls because properties allow data hiding, can be versioned, and are supported by visual designers such as Visual Studio .NET. The accessor functions can perform additional program logic in addition to setting or retrieving a property. If you are not familiar with properties, see Properties Overview.

Properties Inherited from Control

For a complete listing of the properties that a control inherits from the Control class, see System.Web.UI.Control. The following list describes some of the commonly accessed properties.

  • Controls — The collection of a control's child controls.
  • ID — A user-supplied identifier for a control.
  • Page — The page that contains the control.
  • Parent — The control whose Controls collection a control belongs to. (Control A is a parent of control B if B is an element of A.Controls).
  • ViewState — A data structure that is sent to the client and back and is generally used for persisting form data across round trips. ViewState is of type StateBag, which is a dictionary that stores data as name/value pairs.
  • EnableViewState — Indicates whether a control maintains its view state across round trips. If a parent control does not maintain its view state, the view state for its child controls is automatically not maintained.
  • UniqueID — The hierarchically-qualified unique identifier assigned to a control by the ASP.NET page framework.
  • ClientID — A unique identifier assigned to a control by the ASP.NET page framework and rendered as the HTML ID attribute on the client. The ClientID is different from the UniqueID because the UniqueID can contain the colon character (:), which is not valid in the HTML ID attribute (and is not allowed in variable names in client-side script).
  • Visible — Determines whether a control is visible on a page.

Properties Inherited from WebControl

If a control derives from WebControl, it inherits additional properties that are related to visual display. For a complete list of properties inherited from WebControl, see System.Web.UI.WebControl. The following list describes some of the commonly accessed properties of WebControl.

  • Font — The font for the control.
  • ForeColor — The foreground color of the control.
  • BackColor — The background color of the control.
  • Height — The height of the control.
  • Width — The width of the control.
  • Attributes — A collection of name/value pairs rendered to the client as attributes. The Attributes property contains the union of declaratively set attributes that do not correspond to properties (or events) of the control and those that are set programmatically.

WebControl also exposes several additional cascading style sheet (CSS) styles as strongly typed properties.

Customizing Inherited Properties

A control can override an inherited property to customize the property. To override a property, you override its accessors. The following example shows a control that derives from Button and overrides the BackColor property so that the background color is always green.

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace CustomControls
{
public class DerivedButton : Button
  {
   public DerivedButton() : base()
       {
          base.BackColor = Color.Green;
       }
   public override Color BackColor
   {
      get
         {
            return base.BackColor; 
          }
      set
         {
             // Do nothing.
         }
   }

  }
}
[Visual Basic]
Option Explicit
Option Strict

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

Namespace CustomControls
   Public Class DerivedButton
      Inherits Button
      
      Public Sub New()
         MyBase.BackColor = Color.Green
      End Sub
      
      Public Overrides Property BackColor() As Color
         Get
            Return MyBase.BackColor
         End Get
         Set
            ' Do nothing.
         End Set
      End Property 
   End Class
End Namespace

You can also change the metadata for a property by overriding (or providing) an attribute. The following code fragment shows how to hide the BackColor property from the property browser of a designer such as Visual Studio .NET by changing the BrowsableAttribute to false.

[Browsable(false)]
public override Color BackColor{...}
[Visual Basic]
<Browsable(False)> _
Public Overrides Property BackColor() As Color
   ...
End Property

See Also

Defining a Property | Property Types | Custom Property Sample

Show:
© 2014 Microsoft