ASP.NET Architecture, Part 1 - Code-Behind Feature
(Page 8 of 8 )
ASP.NET’s code-behind feature separates business logic from the presentation layer. In order to take advantage of this technique, derive from the Page class. Then place your code in an .aspx file and insert a reference to another file containing the business logic, for example:
<%@ Page Language="vb"%>
<%@ Register TagPrefix="ASPNETSBtn"
TagName="ClientName" Src="RegisterClient" %>
NOTE: Any file referenced with thesrc attribute of the Page directive is compiled into a separate assembly and added to the list of referenced assemblies when the page is compiled. Additionally, an advantage of using thesrc attribute for the code-behind file is the ease with which the developer can update the code-behind file by replacing the file. Subsequently, the next time a page is requested, ASP.NET recompiles the file.
The following example contains HTML, text, and several lines of VB .NET code. The web form has two Textbox controls named FirstName and LastName. The form also contains two RequiredFieldValidator controls that prevent you from submitting the form to the server without entering data in the FirstName and LastName Textbox controls.
NOTE: The <asp: TextBox> tag informs the compiler that this ASP.NET page contains a text box control for execution on the server.
A typical ASP.NET page looks like this:
<%@ Page Language = "vb" AutoEventWireup="false"
Codebehind = "WebForm1.aspx.vb" Inherits="IFCE.WebForm1"%>
<html><head><title>Registering a new client</title></head>
<body>
<form runat="Server">
<p>FirstName:
<br><asp:TextBox id="firstName" runat="Server"/>
<asp:RequiredFieldValidator ControlToValidate="firstName"
Runat="Server"/>
<p>LastName:
<br><asp:TextBox id="lastname" runat="Server"/>
<asp:RequiredFieldValidator
ControlToValidate="lastname"
Runat="Server"/>
<p>
<asp:button text="Submit Form" Runat="server"/>
</form>
</body>
</html>
NOTE: If the <form> element is not present on the page, both web controls and HTML controls will not be able to participate in page post backs, nor will they be able to save their state in the page’s ViewState. They will continue to function otherwise.
It is unwise to bypass one of the key benefits that ASP.NET offers, namely, preserving state between client page requests. Always add the <form> element to your web forms page. If a post-back event occurs, any state stored in hidden fields rendered to the form is retrieved and sent back to the server for rendering the new page to the browser. The user views the newly rendered .aspx page as though it were the original page. In reality, they are two individually unique pages.
Come back this Friday, 27/02/2004, for part two of ASP.NET Architecture. This is chapter six of .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). Buy this book now.
|
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |