ASP.NET - server controls

Server controls are tags that can be understood by the server.

Limitations of Classic ASP

The following code is copied from the previous section:

<html>
<body style="background-color:#e5eecc; text-align:center;">
<h2>Hello CodeW3C.com!</h2>
<p><%Response.Write(now())%></p>
</body>
</html>

The code above reflects the limitations of Classic ASP: code blocks must be placed at the position where output is needed.

Through Classic ASP, we cannot separate executable code from HTML itself. This makes the page difficult to read and maintain.

ASP.NET - server controls

Through server controls, ASP.NET has solved the problem of 'spaghetti code' described above.

Server controls are tags that can be understood by the server.

There are three types of server controls:

  • HTML server controls - traditional HTML tags
  • 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 can be understood by the server.

HTML elements in ASP.NET are processed as text. To make these elements programmable, you need to add The 'runat="server"' attributeThis property indicates that this element is a server control. At the same time, you need to add an id attribute to identify the server control. This id can be used to operate the server control at runtime.

Note:All HTML server controls must be located 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 controls included in it can be accessed by server scripts.

In the following example, we declared an HtmlAnchor server control in the .aspx file. Then we operated on the HRef property of the HtmlAnchor control in an event handler. The Page_Load event is one type of event that ASP.NET can understand:

Note:An event handler (event handler) is a subroutine that executes code for a given event.

<script runat="server">
Sub Page_Load
link1.HRef="http://www.codew3c.com"
End Sub
</script>
<html>
<body>
<form runat="server">
<a id="link1" runat="server">Visit CodeW3C.com!</a>
</form>
</body>
</html>

Tip:The executable code itself has been moved outside of HTML.

ASP.NET - Web server control

Web server controls are special ASP.NET tags that the server can understand.

Similar to HTML server controls, Web server controls are also created on the server, and they also require the runat="server" attribute to take effect. However, Web server controls do not need to map any existing HTML elements; they represent more complex elements.

The syntax for creating a Web server control is:

<asp:control_name id="some_id" runat="server" />

In the following example, we declared a Button server control in the .aspx file. Then we created an event handler for the Click event that can modify the text on the button:

<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="You clicked me!"
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Button id="button1" Text="Click me!" runat="server" OnClick="submit"/>
</form>
</body>
</html>

ASP.NET - Validation server control

The Validation server control is used to validate user input. If the user input does not pass validation, an error message will be displayed to the user.

Each validation control performs a specific validation type (such as validating a specific value or a range of values).

By default, page validation is executed when a Button, ImageButton, or LinkButton is clicked. You can prevent a button control from being validated when clicked by setting the CausesValidation property to false.

The syntax for creating a Validation server control is:

<asp:control_name id="some_id" runat="server" />

In the following example, we declare a TextBox control, a Button control, and a RangeValidator control in the .aspx file. If the validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control:

<html>
<body>
<form runat="server">
Enter a number from 1 to 100:
<asp:TextBox id="tbox1" runat="server" />
<br /><br />
<asp:Button Text="Submit" runat="server" />
</p>
<p>
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="100"
Type="Integer"
Text="The value must be from 1 to 100!"
runat="server" />
</p>
</form>
</body>
</html>

Show This Example