INTRODUCING ASP.NET
Objectives
Objectives
In this lesson, you will learn to:
INSTRUCTOR NOTES
The students are well versed with the .NET Framework and Visual Basic .NET as they
have completed the Developing Enterprise Wide Applications – I module, which is
mapped to Microsoft Exam 70-306. You can start the session by asking them some
questions on .NET Framework that are included in the pre-assessment questions of
this lesson. Students are also expected to know Visual C# syntax before doing this
module.
Lesson Overview
This lesson will help students to become familiar with the ASP.NET concepts. In
addition, students will learn to create an ASP.NET Web application by using the Visual
Studio .NET Integrated Development Environment (IDE). You should highlight the
following concepts during the session:
Setup Requirements
Ensure the following before conducting the session:
Windows 2003 Server, Internet Information Server (IIS) 6.0, and Visual
Studio.Net are installed on the faculty node.
You have copied the all the folders from
the TIRM CD-ROM under the
\DataFiles for Faculty\Cycle 1\OCR1 folder.
Pre-Assessment Questions
1. In .NET, codes are compiled into ________.
a. ByteCode
b. Microsoft International Language
c. BitCode
d. Microsoft Intermediate Language
a. Overloading
b. Overriding
c. Inheritance
d. Structured programming
Introducing ASP.NET
INSTRUCTOR NOTES
You should start the session with a discussion on the evolution of computer technology
from the viewpoint of how it has evolved from a stand-alone computing environment
to a fully distributed computing environment of the Internet. While discussing the
evolution, you should talk about what a Web server is and how it serves the requests
sent by the client browser. Here, you should also mention the benefits of both client-
side and server-side scripting.
Introducing ASP.NET
A dynamic Web application can have client side scripting, server side scripting, or
both.
Client side scripting enables you to develop Web pages that can dynamically respond
to user input without having to interact with a Web server. For example, if there is a
Web application that requires users to enter the user name and the password before
displaying the home page, you need to ensure that a user does not leave the user
name and password fields blank. To check whether a user has left the user name and
password fields blank, you can write client side scripts that execute on the client
computer. A client side script helps reduce network traffic because it does not need to
interact with a Web server to provide dynamic response to user input. In addition to
providing dynamic content, client side scripting speeds up the response time of a Web
application. This happens because a Web server is not overloaded with the job of
processing the client side script for every client. Scripting languages, such as VBScript
and JavaScript, are used to write client side scripts.
Server side scripting provides dynamic content to users based on the information
stored in a remote location, such as a back-end database. It includes code written in
server side scripting languages, such as Active Server Pages (ASP) and Java Server
Pages (JSP). A server side script is executed on a Web server. When a browser
requests for an HTML page containing a server side script, the Web server to which
the request is sent, first processes the script and then sends the results to the
Introducing ASP.NET
Understanding ASP.NET
• ASP.NET is a standard HTML file that contains embedded server-side scripts.
• ASP.NET provides the following advantages of server-side scripting:
• Enables to access information from data sources that are stored on a Web
server or a computer that is accessible to a Web server
• Enables to use a set of programming code called templates to create HTML
documents
• Enables to separate HTML design from the data retrieval mechanism
INSTRUCTOR NOTES
While explaining the features of ASP.NET, you should bring out the relevance of .NET
Framework and discuss how ASP.NET fits in the overall picture of .NET Framework.
You should discuss the how to code-behind feature helps to separate presentation
code and program logic code, as it is critical for the students to know.
Introducing ASP.NET
ASP.NET, which is the .NET version of ASP, is built on the Microsoft .NET Framework.
Microsoft introduced the .NET Framework to help developers create globally
distributed software with Internet functionality and interoperability.
The following figure displays the elements of an ASP.NET application and how the
elements fit in the broader context of the .NET Framework:
Introducing ASP.NET
You create an ASP.NET application by using the tools integrated with the Microsoft
.NET Framework. After you create an ASP.NET application, the ASP.NET files need to
be stored on an IIS server, which is the Web server for the Windows platform. An IIS
server processes the ASP.NET files from top to bottom and then executes the scripts.
Finally, the results are sent to the Web browser that requested the ASP.NET file.
As depicted in the preceding diagram, to execute an ASP.NET file, the following steps
are taken:
1. A Web browser sends a request for an ASP.NET file to a Web server by using a
Uniform Resource Locator (URL).
2. The Web server, IIS, receives the request and retrieves the appropriate
ASP.NET file from the disk or memory.
3. The Web server forwards the ASP.NET file to the ASP.NET script engine for
processing.
4. The ASP.NET script engine reads the file from top to bottom and executes any
server side script it encounters.
5. The processed ASP.NET file is generated as an HTML document, and the
ASP.NET script engine sends the HTML page to the Web server.
6. The Web server then sends the HTML page to the client.
7. The Web browser interprets the output and displays it.
A Web server generates and sends only the HTML output to the client. As a result, it
helps to hide the code of the ASP.NET file from the users who access an ASP.NET Web
page.
Introducing ASP.NET
Features of ASP.NET
• The features of ASP.NET are as follows:
• Compiled Code
• Enriched Tool Support
• Power and Flexibility
• Simplicity
• Manageability
• Scalability
• Security
• Extensibility
In addition to hiding script commands, ASP.NET has the following advanced features
that help develop robust Web applications:
Compiled Code: The code written in ASP.NET is compiled and not interpreted.
This makes ASP.NET applications faster to execute than other server side scripts
that are interpreted, such as the scripts written in a previous version of ASP.
Enriched Tool Support: The ASP.NET Framework is provided with a rich toolbox
and designer in the Visual Studio .NET IDE. Some of the features of this
powerful tool are the "what you see is what you get" (WYSIWYG) editor,
drag-and-drop server controls, and automatic deployment.
Power and Flexibility: ASP.NET applications are based on CLR. As a result, the
power and flexibility of the .NET platform is available to ASP.NET application
developers. ASP.NET applications enable you to ensure that the .NET Framework
class library, messaging, and data access solutions are seamlessly accessible on
the Web. ASP.NET is also language-independent. As a result, you can choose
any .NET language to develop your application.
Simplicity: ASP.NET enables you to build user interfaces that separate
application logic from presentation content. In addition, CLR simplifies
application development by using managed code services, such as automatic
Introducing ASP.NET
• Web Forms
• Web Services
INSTRUCTOR NOTES
You should not discuss the programming models in detail in this section, as students
already have the knowledge of forms and web services. Moreover, these models will
be covered in detail in later sessions.
Web Services
Web Services allow you to share business logic with various applications using the
Internet. For instance, a Web Service can be built to provide information about the
climatic conditions of various places in a country. A travel agency building a
commercial Web site can use this Web Service to present climatic information to their
customers.
A Web Service is defined as a program unit that can be accessed by other applications
through the Internet. It can be called by any application over Hypertext Transfer
Protocol (HTTP). Web Services make use of Extensible Markup Language (XML) to
transfer data through HTTP. They combine the features of component-based
development and the Web based applications.
Introducing ASP.NET
INSTRUCTOR NOTES
This section discusses the general steps to create an ASP.NET application. The
demonstration comes later. While taking this session, you can talk about various
concepts of ASP.NET by taking a small example and showing the students how an
ASP.NET application is created. Students are already familiar with the Visual Studio
.NET IDE as they have developed applications using Visual Basic .NET in Developing
Enterprise Wide Applications –I module.
While creating an ASP.NET application, you can also show them the different files,
which are generated.
The Design and HTML view can also be shown to the class by clicking on the
respective tabs of the IDE, which will help them to understand the concepts better.
The Visual Studio .NET IDE provides a robust and easy environment to create an
ASP.NET application. Designing an ASP.NET page is as easy as dragging and dropping
various controls from the toolbox on to the page. In addition, the VS.NET IDE
automatically generates the basic code of an ASP.NET application. It is pertinent to
Introducing ASP.NET
Introducing ASP.NET
The Visual Studio .NET IDE provides two different views of a Web form:
Design view
HTML view
Design View
The Design view represents the user interface of the Web forms page. You can
directly place controls from the toolbox to the Web forms page in the Design view. By
default, the page layout of the Web forms page is GridLayout. You can change the
page layout to FlowLayout in the DOCUMENT Properties dialog box. To open the
DOCUMENT Properties dialog box, right-click the page in the Design view and
select Properties from the shortcut menu.
The following figure displays the Design view of an ASP.NET page, which contains a
text box, label, and command button:
HTML View
The HTML view represents HTML code of an ASP.NET Web form. To open the HTML
view, click the HTML tab in the designer. If the Web application project is a Visual
Basic project, the scripting language used on the HTML page is Visual Basic. If the
Web application project is a C# project, the scripting language used on the HTML page
is C#.
The following figure displays the HTML view of the page that was displayed in the
Design view in the preceding figure:
Introducing ASP.NET
Namespace Description
System Contains classes for implementing base data
types. In addition, it contains classes for working
with date and time.
System.Collections Contains classes for working with standard
collection types, such as hash tables and array
lists.
System.ComponentModel Contains classes for implementing both design-
time and run-time behaviors of components and
controls.
System.Data Contains classes for implementing the ADO.NET
architecture.
©NIIT Developing Web Applications Using ASP.NET Lesson 1A / Slide 24 of 40
The code-behind feature of ASP.NET enables you to divide an ASP.NET page into two
files. One file contains the presentation content and the other file, which is called the
code-behind file, contains all the application logic. The presentation content consists of
user interface elements, and the code consists of the event-handling procedures to
handle the events that are fired by these controls. The presentation content is stored
in a file with .aspx extension and the code is stored in a separate file with the .cs
extension.
The code-behind feature of ASP.NET offers a number of advantages:
Makes the code easy to understand and debug by separating the application
logic from HTML tags.
Allow the separation of effort between the graphic designers who design pages
and the software engineers who write code.
Eliminate the issues of browser incompatibility
by allowing code files to exist on
the Web server and supporting the Web pages to be compiled on demand.
The code-behind feature of ASP.NET implements multiple inheritance to create a Web
page. When you create a Web application in ASP.NET, Visual Studio .NET creates a
separate .aspx file and a class file by default. The class file that Visual Studio .NET
automatically creates is the code-behind file. In a code-behind file, you will find the
following entry by default:
public class WebForm1: System.Web.UI.Page
INSTRUCTOR NOTES
The purpose of this topic is to make student understand the meaning of the auto-
generated code. Explain in brief the function of each section of auto-generated code.
In the presentation file, .aspx, and the code-behind file,.cs, ASP.NET automatically
generates some lines of code. You can view the presentation file in the HTML mode
and the code-behind file by using Code Editor.
In the HTML mode, the first line of an .aspx file contains the following line of code:
<%@ Page language="c#"
Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"
Inherits="WebApplication1.WebForm1"%>
The @ Page directive is used to specify the page attributes based on which a page will
be created. The different page attributes used in @ page directive are:
Language: Specifies the .NET Framework supported language.
AutoEventWireup: Indicates whether the page framework calls page events and
methods automatically for an ASP.NET page. Page events are the events, which
are associated to an ASP.NET page lifecycle. In the Visual Studio .NET
environment, the default value of the AutoEventWireup attribute is set to false.
A false value indicates that you must explicitly write code to bind events related
to a page, such as the Load event of a page to the Page_Load()method.
Codebehind: Specify the code-behind file for a page.
After the list of namespaces, the code-behind file contains the project namespace
block. The project namespace is defined using the following declaration:
namespace WebApplication1
{
// code here
}
The project namespace block contains the WebForm1 class. Notice that the project
namespace is defined so that other Web applications can access the functions of the
WebForm1 class declared in this project. Within the project namespace, the WebForm1
class is defined using the following declaration:
public class WebForm1 : System.Web.UI.Page
Page Events
Introducing ASP.NET
Page Events
• ASP.NET uses an event-driven model of programming. The following table
lists some of these events:
You should discuss the various events that are associated with an ASP.NET page and
clearly explain the concept of postback to the students.
With each event that is raised, you may have some tasks to accomplish. You can
accomplish these tasks by writing code in special procedures called event-handling
procedures or simply event handlers. For example, you can write the page
initialization code in the Init or Load event of the Web page. The Page_Init event
handler is the event handler for the Init event of the page, and Page_Load is the
event handler for the Load event of the page. There is a difference between the Init
event and the Load event. When the Init event is fired, the controls are loaded with
Introducing ASP.NET
Object Description
Application Provides methods and properties to access information
related to the entire Web application. Information is
stored in either variables or objects.
Request Provides methods, properties, and collections to access
the information about the client requesting the page.
Response Provides methods, properties, and collections to access
information related to the response generated by the
server.
Server Provides methods and properties to access information
related to the server.
Session Provides methods, properties, and collections to access
information related to the user’s session.
While covering this topic, the focus should be more on the purpose of the various
intrinsic objects rather than getting into details about the implementation of these
objects.
ASP.NET has a number of built-in objects that run on a Web server and are
programmed using methods, properties, and collections. These objects are made
available to the Page object, which is an instance of the System.Web.UI.Page class
from which all ASP.NET pages are inherited.
The following table describes the ASP.NET built-in objects:
Object Description
Application
Provides methods and properties to access information
related to the entire Web application. Information is
stored in either variables or objects.
Request
Provides methods, properties, and collections to access
the information about the client requesting the page.
Response
Provides methods, properties, and collections to access
information related to the response generated by the
server.
Server
Provides methods and properties to access information
related to the server.
Session
Provides methods, properties, and collections to access
information related to the user’s session.
The following table describes some of the properties of the Application object:
Properties Description
Count
Returns the number of objects in the
HttpApplicationState collection.
Item
Provides access to an object in the
HttpApplicationState collection.
You can use the following code snippet to create an Application object having the
name, MyName, which stores the value, John:
Application["MyName"]= "John";
You can use the following code snippet to lock the application object, retrieve the
value from the MyName Application object created in the preceding code, and finally
release the lock:
Application.Lock();
string Local_name= Application["MyName"];
Application.UnLock();
Properties Description
You can use the following code snippet to display the client information such as the IP
address, browser name, and browser version on the page:
void Page_Load()
{
//Using the Browser property of the Request object to obtain an
instance //of HttpBrowserCapabilities which contain the details
about the client //browser
HttpBrowserCapabilities bc = Request.Browser;
Properties Description
The following table describes some of the methods of the Response object:
Method Description
Property Description
The following table describes some of the methods of the Server object:
Method Description
public void ClearError() Used to clear the last error that has
occurred on the server.
Property Description
The following table describes some of the methods of the Session object:
Method Description
public void Remove( string name) Used to delete an item from the
session-state collection.
Introducing ASP.NET
• Directives
• Code declaration blocks
• Code render blocks
• Server-side comments
• Server-side Include directives
INSTRUCTOR NOTES
HTML codes are discussed in the following topics to give students a better
understanding of the structure of an ASP.NET Page. Focus should be more on the
function of each section an note on the different element in each section. Moreover,
the past trend of Microsoft Exam 70-315 shows that few questions are asked on HTML
codes.
Directives
A directive specifies how an ASP.NET page is compiled. The beginning of a directive is
marked with the characters, <%@, and the end of the directive is marked with the
characters, %>. The most frequently used directives are Page directives and Import
directives.
Page directives are used to specify the default programming language of a page. For
example, if you want to change the programming language of an ASP.NET page from
Visual Basic to C#, you will use the language attribute of the page directive, as shown
here:
<%@ Page Language="C#" %>
The Import directive is used to import additional namespace to a Web Application. For
example, if you want to send an e-mail from an ASP.NET page, you need to use the
Send method of the SmtpMail class. The SmtpMail class is included in the
System.Web.Mail namespace, which is not imported to a Web application by default.
To explicitly import this namespace, you need to use the import directive, as shown
here:
<%@ Import Namespace="System.Web.Mail" %>
Introducing ASP.NET
INSTRUCTOR NOTES
Explain the use of style sheets for implementing consistent layout in Web application.
Also explain in which scenario each type of style sheet is used.
Styles define the appearance of an HTML element. For example, you can define a color
or a font to be used for an element, such as a paragraph <P>. You can also make that
element bold or italicized.
Introducing ASP.NET
You can define styles either within an HTML document or in an external file. This
external file can be attached to any HTML document. There are three ways to add
style sheet properties to a Web form. They are:
Using an external (linked) style sheet
Using an embedded style sheet
Using an inline style sheet
Introducing ASP.NET
INSTRUCTOR NOTES
The solution for this demo is stored in the \TIRM\Data Files For Faculty\ Cycle
1\OCR1\WebShoppeSite folder. Double-click the WebShoppeSite.sln file to view
the solution.
Problem Statement
The WebShoppe Mall has decided to launch the WebShoppe Web site. The WebShoppe
wants you to design the new user sign-up form to register the new users for accessing
the WebShoppe Web site. The sign-up form should have the first name, last name,
user name, password, confirm password, address, city, state, postal code, country,
telephone number, and e-mail id fields. The form should display a welcome message
when the registration is successful.
Hint: Apply styles by using Cascading Style Sheets to enhance the appearance of the
Web form.
TextBox txtfname
TextBox Txtlname
TextBox Txtuname
TextBox txtpassword
TextBox txtcpassword
TextBox txtaddress
TextBox Txtcity
TextBox Txtstate
TextBox Txtpcode
DropDownList ddlcountry
TextBox Txttelno
TextBox Txtemail
3. To add a new Web form, select the File Æ Add New Item command, as
displayed in the following figure:
if(this.IsPostBack)
{
Response.Redirect ("WebForm2.aspx");
}
}
4. Click on the WebForm2.aspx and click on the HTML tab for the HTML view.
5. Add the following statement within the <Body></Body> tag:
<h1>Welcome, You have successfully registered</h1>
Introducing ASP.NET
Summary
In this lesson, you learned that:
• A Web application can have client-side scripting, server-side scripting, or both.
• ASP.NET, the .NET version of ASP, is built on Microsoft .NET Framework. After
creating an ASP.NET application, the ASP.NET files need to be stored on the IIS
server.
• The features of ASP.NET are:
• Compiled Code
• Enriched Tool Support
• Power and Flexibility
• Simplicity
• Manageability
• Scalability
• Security
• Customizability
• Extensibility
Summary (Contd.)
• ASP.NET provides two types of programming models:
• Web Forms
• Web Services
• Web Forms enable you to create form-based dynamic Web pages.
• A Web Service is defined as a program unit that can be accessed by other
applications through the Internet.
• The following are the files that get generated when you create an ASP.NET
application:
• AssemblyInfo.cs
• Global.asax
• Web.config
• WebApplication.vsdisco
• WebForm1.aspx
Summary (Contd.)
• Web forms have two views:
• Design view
• HTML view
• The code-behind feature of ASP.NET enables you to divide an ASP.NET page into
two files:
• The presentation file that stores the user interface contents.
• The code-behind file that stores the application logic.
• The sequence of events that are fired during the life cycle of a Web page are:
• Init
• Load
• Unload
Summary (Contd.)
• Some of the built-in intrinsic objects are:
• Application
• Request
• Response
• Server
• Session
• The structure of an ASP.NET page includes:
• Directives
• Code declaration blocks
• Code render blocks
• Server-side comments
• Server-side Include directives
• A style sheet is a document, which defines a group of styles.
Summary (Contd.)
• The three ways to add a style sheet to a Web page are:
• Using an inline style sheet
• Using an embedded style sheet
• Using an external (linked) style sheet