Web Forms controls



Server controls are labels that the server can understand.


Limitations of Classic ASP

The code listed below is copied from the previous chapter:

<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3CSschool.cc!</h2>
<p><%Response.Write (now())%></p>
</center>
</body>
</html>

above The code reflects a limitation of classic ASP: the code block must be placed where you want the output to appear.

With classic ASP, it is impossible to separate the executable code from the HTML page. This makes the page difficult to read and difficult to maintain.


ASP.NET - Server Controls

ASP.NET has solved the above-mentioned "spaghetti code" problem through server controls.

Server controls are labels that the server can understand.

There are three types of server controls:

  • HTML Server Control - Created HTML tags
  • Web Server Control - New ASP.NET tags
  • Validation Server Control - Used for input validation

ASP.NET - HTML Server Control

HTML server control is an HTML tag that the server understands.

HTML elements in ASP.NET files are processed as text by default. To make these elements programmable, add the runat="server" attribute to the HTML element. This attribute indicates that the element will be treated as a server control. At the same time, you need to add the id attribute to identify the server control. The id references a server control that can be used to manipulate the 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 must 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. We then manipulate the HtmlAnchor control's HRef property 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:

<script runat="server">
Sub Page_Load
link1.HRef="http ://www.w3cschool.cc"
End Sub
</script>

<html>
<body>

<form runat= "server">
<a id="link1" runat="server">Visit W3CSschool.cc!</a>
</form>

</ body>
</html>

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 the server understands.

Just like 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 necessarily map any existing HTML elements; they can 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 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:

<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

Validation Server controls are used to validate user input. If user input fails validation, an error message will be displayed to the user.

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

By default, page verification will be performed when the Button, ImageButton, and LinkButton controls are clicked. You can set CausesValidation to false to prevent the button control from validating when clicked.

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 validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control:

Example

<html>
<body> ;

<form runat="server">
<p>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>