This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
ASP.NET ASP.NET has been greatly improved over the ASP technology in .Net. Some of the important features of ASP.NET and the improvements over the classical ASP technology include: • ASP.NET is completely compiled code on the server side as opposed to interpreted code in the traditional ASP technology. This helps improves the performance of the web site greatly. • ASP.NET is completely object-oriented as the entire code is written in one of the .Net languages (C# being the most common and most powerful). Traditional ASP used the ActiveX/COM technology to create tiered architectures and give the server-side the code somewhat of an object-oriented feel. Traditional ASP also used ActiveX/COM components to accomplish sophisticated tasks such as sockets, sending emails, uploading files to the server etc.. ASP.NET eliminates the need to use ActiveX/COM components as the entire framework class library is available to accomplish any sophisticated task. • ASP.NET provides a clean separation between the GUI and the server-side code. Each web page typically has two parts to it: a .aspx file containing the HTML presentation, and a code behind file (with .cs or .vb extension) containing the related server-side code. Thus by default, the ASP.NET provides a 2-Tier architecture which can be easily implemented as a multi-tier design as the complexity of the web application increases. • ASP.NET further improves the performance by providing powerful server-side caching of the executed code for a faster response to the client. • The associated ADO.NET technology for dealing with databases also has smart caching of records available in the form of the dataset for a very high performance data driven web application. • ASP.NET is backwards compatible with the classical ASP technology. • ASP.NET has a much easier deployment of web applications. Simply copy the web application directory to a target machine and set up the appropriate virtual directory to run the application. • ASP.NET can use the ActiveX/COM components if needed through the RCW (Runtime Class Wrapper) feature of .Net. Although all new development in ASP.NET can be accomplished without the ActiveX/COM components. • For creating scalable, tiered architectures, ASP.NET still uses the COM+ technology. However, the COM+ programming is made much simpler by the use of special attributes that can be specified for the .Net classes that will be converted to COM+ components. • ASP.NET provides excellent client-side data validation in the form of specialized validators. These validators generate the appropriate client-side Javascript code for data validation on the client side. Thus the need to program in Javascript directly on the client-side is greatly reduced. • ASP.NET provides a powerful server-side event driven model for web application development where different HTML elements (server controls) on the client-side
2
• •
•
•
trigger events on the server-side (the associated code behind file for the aspx file contains the event handlers for these events). ASP.NET provides a very easy way to maintain the state of client-side elements once a server-side event is triggered by one of these elements and the form is submitted to the server. ASP.NET provides an AutoPostBack property for server-side controls so that if anything is changed in the control, the data is posted to the web server. For example, if a checkbox is checked or unchecked, the server-side event handler can be triggered if its AutoPostBack property is set to true. Although Visual Studio.Net is not required for developing ASP.NET applications, it makes the web application development much easier by providing an array of server-side controls, their visual placement and writing of the skeleton code for the event handlers for these. ASP.NET and Visual Studio.NET provide powerful support for creating web services and web services based distributed applications.
We will examine some of the above features of ASP.NET in detail and provide appropriate examples to clarify the concepts.
Understanding Server-side Controls in ASP.NET: As mentioned earlier, ASP.NET provides a powerful new concept in terms of server-side controls and the associated event-driven programming model. In traditional ASP, when we created a form, there needed to be a submit type button, and further the action statement in the form indicated what will be target page that will process this submitted form e.g., a traditional ASP page might look as, <% @LANGUAGE=VBSCRIPT %> <% 'Login.asp – traditional ASP example %> <TITLE> Login Form
Web Site Logon
Please Specify User ID and Password
The target of this page i.e., VerifyLogin.asp might look as:
3 <% @LANGUAGE=VBSCRIPT %> <% 'VerifyLogin.asp – traditional ASP example %> <TITLE> Verifying Login <script language=Javascript>
Verifying User ID and Password
<% dim uid, pwd uid = Request("userID") pwd = Request("password") ' a real application will check the database to see if the submitted userID and password exist ' if so welcome the user else reirect the user to the login page if (uid = "123") and (pwd="webclass") then Response.write("Welcome :" & Request ("userID")) else Response.write("Incorrect User ID and Password, Click on the following button to try again") Response.write("") End if %>
Now let us take a look at the same example in ASP.NET using the server controls and the code behind page. Create a new ASP.NET web application. Name the project Login as shown below i.e., set the location to be http:://localhost/Login.
4 From the project explorer (top right hand corner), change the name of the WebForm1.aspx file to Login.aspx. We will use an HTML table to align the server controls in the Login form. Click on the HTML tab and double click on the table element. This will end up drawing a default 3x3 table.
By first selecting a cell in the table and then right clicking on it, you can choose to delete rows or columns. Similarly by right clicking on a selected cell and clicking on the properties, you can make a row span more than one column. Modify the table to have 3 rows and two columns such that the last row spans two columns, as shown below.
5
Type the text “User ID” and Password in the two left cells of the first column of the table. Then select the Web Form controls from the toolbox. First click on the top right hand cell of the table, then double click on the text box control. Similarly drop another text box control in the cell below it, and a button in the last cell. Select the first text box, give an ID of “txtUserID” to the first text box, and similarly after selecting the second text box, give it an ID of “txtPassword”. Set the TextMode property of the text box to “Password”. Select the button, and from its properties, give it an ID of cmdLogin and Text property of “Login”. Set the alignment of the last cell in the table to be horizontal center. Select the entire table and move it a few lines below from the top left hand corner of the page. By right clicking on the table and selecting properties, you can set the border size of the table to be zero pixels so that the table does not appear in the browser. Double click on the Login button to write a server-side event handler for it. Type the following code in the click event handler of the button. Note that the code is typed in the “code behind” file which has a name of “Login.aspx.cs”. using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
6
namespace Login { /// <summary> /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtUserID; protected System.Web.UI.WebControls.TextBox txtPassword; protected System.Web.UI.WebControls.Button cmdLogin; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.cmdLogin.Click += new System.EventHandler(this.cmdLogin_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion
private void cmdLogin_Click(object sender, System.EventArgs e) { string uid, pwd; uid = txtUserID.Text; pwd = txtPassword.Text; //a real application will check the database to see //if the submitted userID and password exist
7 //if so welcome the user else reirect the user //to the login page if ((uid == "123") && (pwd == "webclass")) Response.Write("Welcome :" + uid); else { Response.Write("Incorrect Login, Click on the button to try again"); Response.Write(""); } } }
}
You need to add a client-side javascript function to the Login.aspx file. Using the HTML tab on this file, you can add the javascript code as shown below. Some of the important sections of code are shown in bold. <%@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="Login.WebForm1" %> WebForm1 <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript"> function Back() { window.history.back(1); }
Taking a look at the Login.aspx file, some of the important things to note are: 1. The aspx file is associated with the code behind file as specified in the first line of the aspx file, e.g., in our Login example, the first line of Login.aspx is: <%@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="Login.WebForm1" %> The CodeBehind and the Inherits attributes above tie the aspx file to the code file and the class contained in it from which the entire aspx file will be derived. 2. The server controls need to be contained in a form which ahs the runat=server specified.