Server Controls


The code illustrates a limitation in classic ASP: The code block has to be placed where you want the output to appear. With ASP it is impossible to separate executable code from the HTML itself.
<html>
 <body bgcolor="yellow">
  <center>
   <h2>Hello W3Schools!</h2>
   <p><% Response.Write( now( ) ) %></p>
   </center>
 </body>
</html>

This makes the page difficult to read, and difficult to maintain. ASP.NET has solved the “spaghetti-code” problem with server controls. Server controls are tags that are understood by the server. Three of the server controls are Other controls such as AJAX and Web Parts server controls will not be covered here.

HTML Server Controls
HTML elements in ASP.NET files, by default, are 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 run time. HTML server controls are HTML tags understood by the server. All HTML server controls must be within a <form> tag with the runat="server" attribute, e.g.,
   <form id="form1" runat="server">
The runat="server" attribute indicates that the form should be processed on the server and the enclosed controls can be accessed by server scripts.