Advantages of ASP.NET Controls
The concept of ASP.NET controls is elucidated to develop smart ASP.NET forms. A “smart” form was earlier developed (in example2) by converting the standard HTML form tags into server side HTML tags. So the question arises as to why use ASP.NET controls at all.
Benefits of using ASP.NET controls
- ASP.NET controls represent the HTML elements of a page in an intuitive object model.
- ASP.NET controls, automatically retain the value of their properties by participating in view state (view state will be explained in the next section).
- ASP.NET controls enable you to separate the design content of a page from the application logic.
- ASP.NET controls enable you to maintain browser compatibility while still supporting advanced browser features such as JavaScript.
Using ASP.NET Controls
In the following section, using ASP.NET controls will enhance Example 2. Modify the page according to the Example 3 and save it with the name DisplayMessage.aspx in the same directory.
| Example3:DisplayMessage.aspx |
<Script Runat="Server">
Sub Submit (Obj as Object, e As EventArgs)
lblName. Text = "Your Name is: <b>" & txtName.text &"</b>"
lblComments. Text="Your Comments are: <b>"&txtComments.text &"</b>"
End Sub
</Script>
<html>
<head><title>AspNetControls.aspx</title></head>
<body>
<b>
Click on the button to view the messages:
</b>
<form Runat="Server">
<asp:textbox id="txtName" runat="server"/><p>
<asp:TextBox
ID="txtComments" textmode="multiline" columns="50" rows="10"
Runat="Server" /><p>
<asp:Button
id="btnSubmit"
Text="Send Comment!"
OnClick="Submit"
Runat="Server" />
<p>
<asp:Label id="lblName" Runat="Server" /><p>
<asp:Label id="lblComments" Runat="Server" />
</form>
</body>
</html> |
| |
The output of this example is shown below:
It is seen that when the form button is clicked, the subroutine named Submit is executed. This subroutine assigns the text “John” and “See the magic of ASP.NET controls” to the Label control with ID lblName and lblComments. It picks this text from the TextBox control with ID txtName and txtComments which is entered by the user.
The Submit subroutine is associated with the Button control through the control’s OnClick method. You can name the subroutine, as you want; there is nothing magical about the name Submit.
The Submit subroutine is declared within the <Script Runat=”Server”> tags at the top of the page. Note that the subroutine accepts two parameters of type Object and EventArgs. The significance of these parameters will is explained below:
Whenever you need to write code to respond to a click button, you use a similar subroutine. The first argument passed to the subroutine represents the object that triggers the event.
In Example 3 the button control with the ID btnSubmit triggers the event. Object parameters can be used to determine the particular object that triggers the subroutine and then responses can be made accordingly.
|