Server controls are tags that are understood by the server.
The listing below was copied from the previous chapter:
The code above illustrates a limitation in Classic ASP: The code block has to be placed where you
want the output to appear.
With Classic ASP it is impossible to separate executable code from the HTML itself. This makes
the page difficult to read, and difficult to maintain.
ASP.NET has solved the "spaghetti-code" problem described above with server
There are three kinds of server controls:
HTML server controls are HTML tags understood by the server.
HTML elements in ASP.NET files are, by default, treated as text. To make these elements
programmable, add a runat="server" attribute to the HTML element. This attribute
indicates that the element should be treated as a server control. The id attribute is added to
identify the server control. The id reference can be used to manipulate the server control at
Note: All HTML server controls must be within a <form> tag with the runat="server"
attribute. The runat="server" attribute indicates that the form should be processed on
the server. It also indicates that the enclosed controls can be accessed by server scripts.
In the following example we declare an HtmlAnchor server control in an .htmlx file. Then we
manipulate the HRef attribute 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 many events
that ASP.NET understands:
The executable code itself was moved outside the HTML.
Web server controls are special ASP.NET tags understood by the server.
Like HTML server controls, Web server controls are also created on the server and they require a
runat="server" attribute to work. However, Web server controls do not necessarily map
to any existing HTML elements and they may represent more complex elements.
The syntax for creating a Web server control is:
In the following example we declare a Button server control in an .htmlx file. Then we create an
event handler for the Click event which changes the text on the button:
Validation server controls are used to validate user-input. If the user-input does not pass
validation, it will display an error message to the user.
Each validation control performs a specific type of validation (like validating against 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 prevent validation when a button control is clicked by setting the
CausesValidation property to false.
The syntax for creating a Validation server control is:
In the following example we declare one TextBox control, one Button control, and one
RangeValidator control in an .htmlx file. If validation fails, the text "The value must be
from 1 to 100!" will be displayed in the RangeValidator control:
Your Query was successfully sent!