Latest web development tutorials

ASP.NET server controls

Server controls are tags understood by the server.


Limitations of classic ASP

Codes listed below are copied from the previous chapter:

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

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

Through classic ASP, want to separate executable code from the HTML page is not possible. This makes the page difficult to read, it is difficult to maintain.


ASP.NET - Server Controls

By ASP.NET server controls, it has solved the above-mentioned "spaghetti code" problem.

Server controls are tags understood by the server.

There are three types of server controls:

  • HTML server controls - HTML tags created
  • Web Server Controls - New ASP.NET tags
  • Validation Server Controls - Input validation

ASP.NET - HTML Server Controls

HTML server controls are HTML tags understood by the server.

ASP.NET file HTML elements, the default is treated as text. To get these elements programmable, add runat = "server" attribute to the HTML element. This attribute indicates that the element will be treated as a server control. Also you need to add the id attribute to identify the server control. id reference can be used to operate the server control at run time.

Note: All HTML server controls must be located within with runat = "server" attribute of the <form> tag.runat = "server" attribute indicates that the form must be processed on the server. It also shows that the controls contained within it can be accessed by server scripts.

In the following example, we declare a HtmlAnchor server control in an .aspx file. Then we operate HRef property HtmlAnchor control in an event handler (an event handler for a given event code execution subroutine) in. ASP.NET Page_Load event is understandable in a variety of events:

<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 W3CSchool.cc!</a>
</form>

</body>
</html>

Executable code itself has been moved outside the HTML.


ASP.NET - Web Server Controls

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 also need runat = "server" attribute to take effect. However, Web server controls do not need to map any existing HTML elements and they may represent more complex elements.

Create a Web server control syntax is:

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

In the following example, we declare a Button server control in an .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 Controls

Validation server controls are used to validate user input. If the user input is not validated, an error message will be displayed to the user.

Each validation control performs one of the specified type of authentication (such as verification of a specified value or a range of values).

By default, when a Button, ImageButton, LinkButton control is clicked, the page performs validation. You can set the CausesValidation to false, the button controls to prevent verification is clicked.

Create a Validation server control syntax is:

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

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

Examples

<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>