Server Controls
The Server controls are defined as user interface elements of a web form.
There are four types of server controls in ASP.NET: -
HTML server controls
An HTML server control represents the normal HTML form elements, such as text-input boxes and buttons, but they’re created on the server, where they can be controlled.
Web controls Web controls are similar to HTML server controls, but they represent more complex user interfaces and are more functional in nature.
Validation controls Validation controls are used to validate user input,
User controls
User controls are custom-built controls that perform some functional piece of work.
(All of these controls will be covered in the next section.)
All server controls possess properties, methods, and events, which make them much more functional than the traditional HTML form elements. They further make it easier for the developer to build user interfaces.
When a server control is built, there is no need to worry about writing HTML code. Whenever the page is requested, server controls will automatically show the HTML that will produce the proper display. For example, the following line creates a Button server control on the server: -
| <asp:Button text “Submit” runat= “server”/> |
When this button is requested, it will show the following HTML to the client:
| <input type “Submit” name= “ctrl1” value= “Submit”/> |
Here the first line is used only by the server- the client gets no view of it (and if it does, the browser would not know what to do because it understands only HTML). The client sees the second line.
These controls provide the UI layout, and give more time to think about UI and its capabilities and less time to worry about the HTML.
ASP.NET knows the capabilities of each browser, therefore it sends the appropriate HTML to each one. For example, if a browser doesn’t support Dynamic HTML (DHTML), ASP.NET won’t send it any. This is known as down-level support because ASP.NET can tone down the HTML output for browsers that don’t support higher-level functionality.
Server Controls Events
Server controls generate multitudes of events. In other words, there are a lot of things a user can do to a server control like click on a button, click a link, fill out a text box, select an item in a list box, and so on. The server must handle all these events, so that the client posts the data to the server every time an event occurs,
There are two ways in which the events are posted to the server: immediately as they occur, or collectively during a single post.
The latter is more efficient because not many posts are sent, which means that less data is travelling back and forth between the client and the server.
In many cases, you would like to use the second method. For example, imagine a user typing his name into a form. Every time he types a letter, an event occurs, You wouldn’t like to post data every time a letter changes, but only when the user has finished entering the data or clicks the Submit button. Events like this are cashed; i.e. they’re stored up on the client until the user decides to post the data. Perhaps at this point the server can evaluate them one at a time.
It is important to know that all server control events in ASP.NET send two pieces of data to the server: an object that represents the control that generated the event, and an object that describes any specific information about the event.
Controls automatically generate events whenever something happens. To handle an event on the server, the control needs to be told what method to be used.
The following example shows the ASP.NET to execute the ClickHandler method on the server upon the occurrence of the Click event:
| <asp: Button runat= “server” OnClick “ClickHandler” /> |
This method would look like this:
1: sub ClickHandler (obj as Object, e as EventArgs)
2: do some task…
3: end sub
As mentioned earlier, line 1 shows the standard event parameter list for ASP.NET server controls. The server parameter can vary depending on the type of event that occurs. The standard parameter list would be used most of the time.
Let’s look at the following example
| Example7:eventhandler.aspx |
<% @ Page Language "VB" %>
<script runat= "server">
Sub Button1_Click (obj as object e as EventArgs)
Label1.Text= "You Clicked <b>" & obj. Text & "</b>"
end Sub
</script>
<html> <body>
<font size= "5"> www.expertrating.com </font><hr>
<p>
<form runat= "server">
<asp: Button runat= "server” Text= "My Button"
OnClick= "Button1_Click" />
<p>
<asp: Label id= Label1 runat= "server" />
</form>
</body>
</html> |
| |
.
|