## ASP.NET Web Forms - Server Controls
* * *
Server controls are tags that the server understands.
* * *
The code listed below is copied from the previous chapter:
Hello !
<%Response.Write(now())%>
The above code reflects the limitations of classic ASP: code blocks must be placed where you want the output to appear.
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 "spaghetti code" problem mentioned above through server controls.
Server controls are tags that the server understands.
There are three types of server controls:
* HTML Server Controls - Traditional HTML tags
* Web Server Controls - New ASP.NET tags
* Validation Server Controls - For input validation
* * *
## ASP.NET - HTML Server Controls
HTML server controls are HTML tags that the server understands.
HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, you need to add the runat="server" attribute to the HTML element. This attribute indicates that the element will be processed as a server control. You also need to add an id attribute 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
The executable code itself has been moved outside the HTML.
* * *
## ASP.NET - Web Server Controls
Web server controls are special ASP.NET tags that the server understands.
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 fails validation, an error message will be displayed to the user.
Each validation control performs a specific type of validation (for example, 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 a 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:
## Example