Lecture notes on ASP.NET
Goal of this lecture: to help the students understand the fundamental concepts of
ASP.NET, so that they can understand the sample code better when they go through the tutorials.
Topic 1: .NET
- .NET is a software development framework with three aims:
- network transparency
- platform independence
- rapid application development
- Virtual machine is a concept to achieve platform independence.
- Traditionally, the source code (e.g. *.c) is compiled into machine code (e.g. *.exe), which can run only on one architecture.
- With virtual machine as a software layer between the OS and the applications,
the source code (e.g. *.java) is compiled into an intermediate version called bytecode (e.g. *.class), which can be interpreted by the VM at run time.
- This is why the same compiled version of Java code can be run on multiple architectures.
- .NET uses a virtual machine called Common Language Runtime (CLR) to support network transparency.
- Main competitor of .NET: J2EE (Java 2 Platform, Enterprise Edition). It includes concepts like JSP and JDBC.
- Which one to choose? For pure Microsoft platform (you are sure that your developed applications will only be run on Microsoft platforms), use .NET. Otherwise, use J2EE.
- Similarities between Microsoft ideas and existing ideas:
- CLR --> Virtual Machine
- .NET --> J2EE
- C# --> Java
- IE --> Netscape
- Windows OS --> MacOS
Topic 2: ASP.NET
- ASP.NET is a technology for developing dynamic, web applications. It is a successor of ASP adapted to fit in the .NET framework.
- One can write ASP.NET code using any programming language supported by the .NET framework. As of 2005, there are 40 supported languages including the three built-in ones: C#, Visual Basic, and JScript. The remaining 37 languages include Perl, but not Java.
- Advantages of ASP.NET over ASP:
- improved performance using (a) caching of pages and application data, and (2) JIT: Just-in-time compilation. Also called dynamic translation. A technique to improve the performance of bytecode execution by translating bytecode into machine code at run time.
- event-driven GUI development fashion. Developers are familiar with this fashion, where they design some forms and then associate each control (e.g. button) with a callback function. To develop earlier web applications (e.g. using CGI and ASP), they need to worry about generating the HTML file and maintaining application data. With ASP.NET, they nicely move back to the GUI fashion.
- faster development supported by a rich set of controls and class library -- most of which are in the .NET class library, to be share with other .NET technologies. The .NET class library contains about 9000 classes.
- A simple ASP.NET page: rename an HTML file with the suffix .aspx.
- Structure of an ASP.NET page:
- page directives: specifies how the page should be processed. Typically at the very beginning of a file. For example,
- <%@ Page Language="C#" %>
Tells that the inline code in the page are written in C#.
- <%@ Import namespace="System.Data.SqlClient" %>
So that you can use something like SqlConnection con = new SqlConnection().
- code section: contains the callback functions and helper routines. For example,
- <script runat="server" language="C#">
code goes here
</script>
Note that without runat="server", the complete <script> tag will be sent to the client as it is, to be understood as a JavaScript code which will be executed at the client side. Also, all script tags that run at server should have the same language.
- <script runat="server" language="C#" src="mycode.cs"/>
There is no code in this <script> tag as the code is in a separate file.
- page layout: the skeleton of the page. It contains:
- HTML tags
- server controls
- [Not recommended!] inline code (in <% ... %>) or declared code (script tag)
- Notes on inline code:
- Unlike declared code, inline code is not pre-compiled and therefore is less efficient.
- Can be as simple as an inline expression. E.g. <%=i%> will be replaced by the value of variable i at run time.
- In ASP.NET, inline code cannot contain any function definition. This is different from ASP.
- Default namespaces:
- There are 14 namespaces that are imported by default. For example:
- System.Web. That's why you can use Response and Request in code.
- System.Web.UI.HtmlControls. That's why you can use HTML controls in your code.
- System.Web.UI.WebControls. That's why you can use web controls in your code.
- Server controls:
- .NET classes that implements GUI controls and render their content as HTML.
- Two types of server controls: HTML controls and web controls.
- A classic HTML tag, if having runat="server", is mapped to an instance of some HTML control class in the namespace System.Web.UI.HtmlControls.
- A web control is prefixed by <asp>. It sort of does the same thing as a HTML control, but typically has more functionality.
- One ASP.NET page must group all server controls under a single <form> tag, which has runat="server".
Topic 3: An example application
Task: design an form which asks the user to input a name and select a category. If the user clicks the lookup button, display a response message.
- The stateless version (does not really do the job!) code.
As a starting point, this plain HTML code shows a name field, a category list, a button, and the response label. What it fails to do is the once the user clicks the lookup button, the response label is unchanged. Furthermore, the user's input is reset.
- The ASP version code.
In the earlier web application development, the developed programs prints out an HTML file to be sent to the client. The ASP version pretty much follows this style. Note how the code uses a for loop to produce each selection option, while using Response.Write() to print out the text "selected" on the selected option. To query the application data, the code uses the property Request.QueryString.
- The Java application (not web application!) code.
You may have found that the ASP style code is messy, as the developer needs to care about the whole flow (for loops, etc.) about printing out the correct HTML document and to care about maintaining the application data. We recall that the GUI programming fashion does not do that. For instance, let's examine this Java code. It produces a form and a callback function and that's it. Can we do the same thing in developing web applications? The answer is yes in ASP.NET.
- The HTML-control version code.
In both the HTML-control version and the web-control version, the page contains a form description and some callback functions -- the same as in the Java program in concept!
In the HTML-control version, the page layout part looks extremely similar to the static HTML file, with a major difference that the form elements all contain runat="server". As we said before, such tags are not regular HTML tags to be sent to the client. Rather, they will be mapped to HTML control objects. Naturally, such objects maintain their state. Therefore, when the user clicks the lookup button, the content of the name field and the selected item in category retains automatically.
- The web-control version code.
Alternatively, you can use web controls, as illustrated in this example.