Microsoft ASP.NET Server Control: Part One
ASP.NET powered a significant portion of web, especially high-end corporate and industrial sites. Just to jiggle your memory – ASP.NET is a web application framework developed by Microsoft that programmers can use to build dynamic web sites, web applications, and XML web services. It is a part of Microsoft’s .NET platform and is the heir to Microsoft’s Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, meaning programmers can write ASP.NET code using any Microsoft .NET language (C#, Visual Basic .NET, J#, F#, IronPhython, IronRuby, windows power shell and C++/CLI).
To understand ASP.NET, first and most of all, you have to get to know its Server Control. ASP.NET server controls are a fundamental part of the ASP.NET architecture. Essentially, server controls are classes in the .NET Framework that represent visual elements on a web form. Some of these classes are relatively straightforward and map closely to a specific HTML tag.
Other controls are much more ambitious abstractions that render a more complex representation from multiple HTML elements. ASP.NET 2.0 keeps the .NET 1.0 controls almost unchanged and adds a shift of new controls. However, I won’t get into all that in this article, rather focus on ASP.NET 2.0. Let’s begin with types of ASP.NET server controls.
Types of Server Controls
ASP.NET offers many different server controls, which fall into several categories, such as:
HTML server controls: These are classes that wrap the standard HTML tags and are declared with the runat=”server” attribute. Apart from this attribute, the declaration for an HTML server control remains the same. Two examples include HtmlAnchor (for the < a > tag) and “HtmlSelect” (for the < select > tag). However, you can turn any HTML tag into a server control. If there isn’t a direct corresponding class, ASP.NET will simply use the HtmlGenericControl class. To create one of these controls in Visual Studio, you need to drag an HTML element from the HTML tab of the Toolbox. Then, right-click the element, and choose “Run As” Server Control to add the runat=”server” attribute.
Web controls: Duplicate the functionalities of the basic HTML tags but have a more consistent and meaningful set of properties and methods that make it easier for the developer to declare and access them. Some examples are the HyperLink, ListBox, and Button controls. In addition, several other types of ASP.NET controls (such as rich controls and validation controls) are commonly considered to be special types of web controls. In Visual Studio, you’ll find the basic web forms controls in the Standard tab of the Toolbox.
Rich controls: These cool, unique, and advanced controls have the capabilities to generate a large amount of HTML markup and even client-side JavaScript to create the interface. Examples include the Calendar, AdRotator, and TreeView controls. In Visual Studio, rich controls are also found in the Standard tab of the Toolbox.
Validation controls: Perhaps the best friend of a developer when he/she is developing a form/data submission process. This set of controls allows you to easily validate an associated input control against several standard or user-defined rules. For example, you can specify that the input can’t be empty, that it must be a number, which must be greater than a certain value, and so on. If validation fails, you can prevent page processing or allow these controls to show inline error messages in the page. In Visual Studio, these controls are found in the Validation tab of the Toolbox.
Additionally there are several other types of server controls, like-Data Controls, Navigation Controls, Web Parts Control, Mobile Controls, and Login Controls etc. Now, that we know the types of ASP.NET server controls, let’s discuss the server control inheritance.
The Hierarchy
You should always keep in mind that all server controls (however, the four controls -Literal, PlaceHolder, Repeater, and Xml derive from System.Web.UI.Control itself) derive from the base Control class in the System.Web.UI namespace. Whether you’re using HTML server controls, using web controls, or creating your own custom controls, this doesn’t change this scenario. The same thing we can say about the Page class from which all web forms derive.
Since all controls derive from the base Control class, you have a basic common denominator that you can use to manipulate any control on the page, even if you don’t know the specific control type. For example, you can use this technique to loop through all the controls on the page and hide each one by setting the visible property to false. Below, I put together a list of most commonly used public methods, events, and members of the Control class with their properties.
Control Class Properties (Public)
ClientID – Gets the server control identifier (a unique name) generated by ASP.NET at the time the page is instantiated.
Controls – Returns the collection (ControlCollection object) of child controls on the page. This is the “first level” of controls, some of which may be container controls that contain additional child controls of their own.
EnableViewState – Gets or sets a Boolean value indicating whether the control should maintain its state across postbacks of its parent page. This property is True by default.
ID – Returns or sets the identifier of the control. In action, this is the name through which we can access the control from the server-side scripts or the code-behind class.
Page – Gets a reference to the Page instance that contains the server control.
Parent – Gets a reference to the control’s parent, which can be the page or another container control.
Site – Gets information about the container that hosts the current control when rendered on a design surface.
Visible – Returns or sets a Boolean value indicating whether the control should be rendered. If False, the control isn’t just made invisible on the client—instead, the corresponding HTML tag is not generated.
Control Class Methods
DataBind() – Binds a data source to the invoked server control and its entire child controls (to the specified data source or expression).
FindControl() – Searches for a child control with a specific name in the current control and all contained controls. If the child control is found, the method returns a reference of the general type Control. If none found, it returns false.
Focus() – Sets input focus to a control.
HasControls() – Returns a Boolean value indicating whether this control has any child controls. The control must be a container tag to have child controls (such as a < div > tag).
RenderControl() – Outputs server control content and stores tracing information about the control if tracing is enabled.
Control Class Events
DataBinding – Occurs when the server control binds to a data source. This event notifies the server control to perform any data-binding logic that has been written for it.
Init – Occurs when the server control is initialized, which is the first step in its life-cycle. Server controls should perform any initialization steps that are required to create and set up an instance. You cannot use view-state information within this event; it is not populated yet. You should not access another server control during this event, regardless of whether it is a child or parent to this control. Other server controls are not certain to be created and ready for access.
These are just the list of commonly used Control class Events, methods, and properties; you can view the complete list of these on MSDN.
Next session of ASP.NET Server Control we will discuss more descriptively on HTML Server Controls.