ASP.NET Web Forms - Server Controls
Server controls are tags that are understood by the server.
Limitations of Classic ASP
The following code listing is copied from the previous chapter: <%Response.Write(now())%>
The code above reflects the limitations of Classic ASP: code blocks must be placed where you want the output to be displayed.
With Classic ASP, it is impossible to separate executable code from the HTML page. This makes the page difficult to read and maintain.
ASP.NET - Server Controls
ASP.NET has solved the problem of "spaghetti code" mentioned above through server controls.
Server controls are tags that are understood by the server.
There are three types of server controls:
HTML Server Controls - HTML tags created
Web Server Controls - New ASP.NET tags
Validation Server Controls - Used for input validation
ASP.NET - HTML Server Controls
HTML server controls are HTML tags that are understood by the server.
HTML elements in an ASP.NET file are, by default, treated as text. To make these elements programmable, add the runat="server" attribute to the HTML element. This attribute indicates that the element is to be treated as a server control. An id attribute is also added to identify the server control. The id reference can be used to manipulate the server control at runtime.
Note: All HTML server controls must be within a <form> tag with the runat="server" attribute. The runat="server" attribute indicates that the form is to be processed on the server. It also indicates that the controls contained within it can be accessed by server scripts.
In the following example, we declare an HtmlAnchor server control in the .aspx file. Then we manipulate the HRef property of the HtmlAnchor control in an event handler (an event handler is a subroutine that executes code for a given event). The Page_Load event is one of the many events understood by ASP.NET:
The executable code itself has been moved outside of the HTML.
ASP.NET - Web Server Controls
Web server controls are special ASP.NET tags that are understood by the server.
Like HTML server controls, web server controls are also created on the server and require the runat="server" attribute to be effective. However, web server controls do not necessarily map to any existing HTML elements; they can represent more complex elements.
The syntax for creating a web server control is:
In the following example, we declare a Button server control in the .aspx file. Then we create an event handler for the Click event to change the text on the button:
ASP.NET - Validation Server Controls
Validation server controls are used to validate user input. If the user input does not pass validation, an error message is displayed to the user.
Each validation control performs a specific type of validation (such as validating a specific value or a range of values).
By default, page validation is performed when a Button, ImageButton, or LinkButton control is clicked. You can set CausesValidation to false to prevent validation when the button control is clicked.
The syntax for creating a validation server control is:
In the following example, we declare a TextBox control, a Button control, and a RangeValidator control in the .aspx file. If validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control: