ASP.NET Server Controls
ASP .NET Server controls are tags that are understood by the server. There
are three kinds of server controls:
- HTML Server Controls
- Web Server Controls
- Validation Server Controls
HTML Server Controls
HTML server controls must be within a <form> tag with the runat="server"
attribute. For example:
<script runat="server">
Sub Page_Load
link1.HRef="http://www.selectagps.com"
End Sub
</script>
<html>
<body>
<form runat="server">
<a id="link1" runat="server">Buy a GPS for your
car from SelectaGPS.com!</a>
</form>
</body>
</html>
In the above example, the codes between tags <script> and </script>
are used to put ASP.NET scripts. And the codes between <html> and </html>
are almost classic HTML codes.
The runat="server" attribute indicates that the form should be processed
on the server. And the Page_Load event is one of many events that ASP .NET understands.
The id attribute is to identify the server control.
Web Server Controls
Like HTML server controls, Web server controls also require a runat="server"
attribute. 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:
<asp:control_name id="some_id" runat="server" />
Let's see the following example. Firstly, we add a Button server control in
the file. Then we create an event handler for the Click event.
<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="Hello, welcome to iTechCollege.com!"
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Button id="button1" Text="Click Here to say Hello"
runat="server" OnClick="submit"/>
</form>
</body>
</html>
When we load the page, and click the button, the text "Click Here to say Hello"
will be displayed.
Validation Server Controls
Validation server controls is used to validate data input by users. If the
input does not pass validation, it will display an error message to the user.
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 Causes Validation property to false.
Like Web server controls, the syntax for creating a Validation server control
also is:
<asp:control_name id="some_id" runat="server" />
In the following example we declare one TextBox control, one Button control,
and one RangeValidator control in an .aspx file. If validation fails, the text
message will be displayed in the RangeValidator control:
<html>
<body>
<form runat="server">
Enter a number from 1 to 6:
<asp:TextBox id="tbox1" runat="server" /><br
/>
<asp:Button Text="Submit" runat="server" /><br
/>
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="6"
Type="Integer"
EnableClientScript="false"
Text="Sorry. The value must be from 1 to 6"
runat="server" />
</form>
</body>
</html>
Server and HTML controls provide overlapping functionality. You can use server
controls or HTML controls on a Web form. You can say that server controls are
a superset of HTML controls.
|