7/30/2019 ASP.net Session 01
1/39
Slide 1 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
With the increased use of the Internet and advances in
information technology, application developers need to
quickly create applications that are accessible over the Web
or a corporate intranet. The applications should also be
efficient and effective.
ASP.NET provides developers with various time saving and
code saving features. One of its key design goals is to make
programming easier and quicker by reducing the amount of
code. In addition, it contains several new server controls,
which eliminate the need for writing voluminous code.
Rationale
7/30/2019 ASP.net Session 01
2/39
Slide 2 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
In this session, you will learn to:
Identify the basics of Web development
Explore ASP.NET
Develop ASP.NET applications
Objectives
7/30/2019 ASP.net Session 01
3/39
Slide 3 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Web applications are programs that can be executed on a
Web server and accessed from a Web browser.
Web applications enable organizations to share and access
information on the Internet and corporate intranets.
A Web application consists of the following types of Webpages:
Static Web page
Dynamic Web page
An application that consists of dynamic Web pages is a
dynamic Web application.A Web application can be made dynamic by using
server-side and client-side scripts.
Introducing Web Development
7/30/2019 ASP.net Session 01
4/39
Slide 4 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Server-side scripting provides users with dynamic content
that is based on the information stored at a remote location,
such as a back-end database.
Server-side scripting includes code written in server-side
scripting languages, such as:Active Server Pages (ASP).
Java Server Pages (JSP).
A server-side script is executed on a Web server.
Server-Side Scripting
7/30/2019 ASP.net Session 01
5/39
Slide 5 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following figure shows the working of server-side
scripts.
Server-Side Scripting (Contd.)
7/30/2019 ASP.net Session 01
6/39
Slide 6 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Client-side scripting:
Enables you to develop Web pages that can dynamically
respond to user input without having to interact with a Web
server.
Provides dynamic content.
Helps reduce network traffic because it does not need to
interact with a Web server to provide dynamic response to user
input.
Speeds up the response time of a Web application.
Scripting languages such as VBScript and JavaScript are
used to write client-side scripts.
Client-Side Scripting
7/30/2019 ASP.net Session 01
7/39Slide 7 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following figure shows the working of client-side scripts.
Client-Side Scripting (Contd.)
7/30/2019 ASP.net Session 01
8/39Slide 8 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Limitations of client-side scripting:
Browser support: Client-side scripting is not supported equally
by all browsers and operating systems.
Isolation: Client-side scripts cannot access server-side
resources.
Security: Client-side scripts are visible to the end users.
Therefore, they can be tampered by malicious users.
Thin clients: Web-enabled devices such as mobile phones,
palmtop computers, and Personal Digital Assistants (PDAs) do
not support client-side scripting such as JavaScript or
VBScript.
Client-Side Scripting (Contd.)
7/30/2019 ASP.net Session 01
9/39Slide 9 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
ASP.NET is a server-side technology that enables
programmers to create dynamic Web applications.
ASP.NET has a number of advanced features such as
simplicity, security, and scalability that help you develop
robust Web applications.The various advanced features of ASP.NET are based on
the .NET Framework.
Exploring ASP.NET
7/30/2019 ASP.net Session 01
10/39Slide 10 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following figure displays the elements of an ASP.NET
application and how the elements fit in the broader context
of the .NET Framework.
ASP.NET in the .NET Framework
7/30/2019 ASP.net Session 01
11/39Slide 11 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following figure depicts how a Web server processes a
request for an ASP.NET file.
Working of an ASP.NET Application
7/30/2019 ASP.net Session 01
12/39Slide 12 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
An ASP.NET application can be created by using any text
editor. However, this is a very time-consuming process.
In addition, this process increases the possibility of inducing
errors that could otherwise be avoided by using a design
tool.
A design and productivity tool, such as Visual Studio,
provides various features that enable the developers to
quickly create robust ASP.NET applications.
Developing ASP.NET Applications
7/30/2019 ASP.net Session 01
13/39Slide 13 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Some of the features of Visual Studio IDE are:
Integrated error checking
The Web form designer
An integrated Web server
Developer productivity enhancementsComplete extensibility
Developing ASP.NET Applications (Contd.)
7/30/2019 ASP.net Session 01
14/39Slide 14 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Website Types
You can use Visual Studio 2008 to create the following
types of websites:
File-system websites
Local Internet Information Services (IIS) websites
Remote IIS websitesFTP sites
Let us see how to create an ASP.NET website
Let us see how to open an existing ASP.NET website
7/30/2019 ASP.net Session 01
15/39Slide 15 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table lists some specialized directories that an
ASP.NET application uses.
Anatomy of an ASP.NET Application
Directory Descript ion
Bin Stores all the compiled .NET components (DLLs) that an ASP. Web
application uses.
App_Code Stores source code files that are dynamically compiled to be used in theWeb application.
App_Browsers Stores browser definition files. Browser definition files are XML files that
define a browser with its properties, capabilities, and features.
App_GlobalResources Stores global resources that are accessible to every page in the Web
application.
App_LocalResources Stores .resx files that are accessible to a specific page only.
App_WebReferences Stores references to Web services that are used by the Web application.
App_Data Stores data such as database files and XML files that are used by the
application.
App_Themes Stores the themes that are used in the Web application.
7/30/2019 ASP.net Session 01
16/39Slide 16 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table lists some important file types that an
ASP.NET Web applications can include.
Anatomy of an ASP.NET Application (Contd.)
File Name Descript ion
Ends with .aspx These are ASP.NET Web pages that contain the user interface and, optionally, the
underlying application code. Users request or navigate directly to one of these pages
to start a Web application.
Ends with .ascx These are ASP.NET user controls. These controls are similar to Web pages, exceptthat the user cannot access these files directly. Instead, they must be hosted inside
an ASP.NET Web page. User controls allow you to develop a small piece of user
interface and reuse it in as many Web forms as you want without repeating code.
Ends with
.asmx
These are ASP.NET Web services. Web services are collections of methods that can
be called over the Internet. Web services work differently than Web pages. However,
they still share the same application resources, configuration settings, and memory.
web.config This is the XML-based configuration file for ASP.NET applications. It includes
settings for customizing features such as security, state management, and memorymanagement.
Global.asax This is the global application file. You can use this file to define global variables
(variables that can be accessed from any Web page in the Web application).
Ends with .cs These are code-behind files that contain C# code. These files allow you to separate
the application logic from the user interface of a Web page.
Ends with .ashx These are default HTTP handlers for all Web handlers that do not have a userinterface and that include the @ WebHandler directive.
7/30/2019 ASP.net Session 01
17/39Slide 17 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Configuration files used in ASP.NET Web applications:
ASP.NET uses a hierarchy of configuration files to keep
application configuration settings separate from the application
code.
A configuration file is an XML file that contains configuration
settings for an application and has a .config extension.
Benefits of a configuration file:
It provides control and flexibility over the way you run
applications.
It eliminates the need to recompile the application every time a
setting changes.
It controls access to protected resources, and the location of
remote applications and objects by defining configuration settings.
Anatomy of an ASP.NET Application (Contd.)
7/30/2019 ASP.net Session 01
18/39Slide 18 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table lists the levels, the corresponding
configuration files, and their description.
Anatomy of an ASP.NET Application (Contd.)
Level File Name Descript io n
Server Settings Machine.config Present at the root of the configuration hierarchy. It
defines global configuration settings for all . Framework
applications.
Root Web Settings Web.config Present in the same directory as machine.config. Itdefines configuration settings for all ASP. applications.
Website settings
(optional)
Web.config Present in the root directory of each IIS website. It
contains settings that are specific to the website.
Application root
Settings (optional)
Web.config Present in the root directory of each application. It
contains settings that are specific to the application.
Application
subfolder (optional)
Web.config Present in a subfolder of the application root. It contains
settings for specific section of a Web application.Server Settings Machine.config Present at the root of the configuration hierarchy. It
defines global configuration settings for all . Framework
applications.
7/30/2019 ASP.net Session 01
19/39Slide 19 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
A Web page is made up of the following two components:
The visual portion: It refers to the static HTML and ASP.NET
server controls that define the layout of a Web page.
The programming logic: It refers to the code that is required to
interact with the Web page.
ASP.NET provides the following two models for managing
the visual elements and code:
Single-file page model
Code-behind page model
ASP.NET Web Page Code Model
7/30/2019 ASP.net Session 01
20/39Slide 20 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The single-file page model:
In the single-file page model, HTML markup of the page and its
programming code are in the same physical .aspx file.
The programming code is contained in a block that
specifies the attribute runat=server.
The single-file page model provides the following advantages:
In pages with less code, keeping the programming code and the
HTML markup in a single file proves to be very convenient while
studying the file.
Pages written by using the single-file model are slightly easier to
deploy or to send to another programmer because there is only
one file.
A single-file page is easier to rename because there is no
dependency between files.
Managing files in a source code control system is slightly easier
because the page is self-contained in a single file.
ASP.NET Web Page Code Model (Contd.)
7/30/2019 ASP.net Session 01
21/39Slide 21 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The code-behind page model:
In the code-behind page model, HTML markup is kept in one
file with extension .aspx and the programming code in another
file with extension .cs or .vb.
The code file contains a partial class, which indicates that the
class contains only some of the total code that makes up thefull class for the page.
The code-behind model provides the following advantages:
It allows a developer to separate the UI display from the UI
processing, thereby enabling the programmer to reuse the
programming logic.
It enables a developer to quickly debug the code because the
code is not clubbed with the UI.
It enables developers to quickly create a Web application by
separating the design and development functions.
ASP.NET Web Page Code Model (Contd.)
Let us see how to add a new Web form to an ASP.NET website
7/30/2019 ASP.net Session 01
22/39Slide 22 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
All Web forms are instances of the ASP.NET Page class,
which is defined in the System.Web.UI namespace.
The Page class inherits the TemplateControl class,
which, in turn, inherits the Control class.
ASP.NET uses an event-driven model of programming,which defines a sequence of events that are raised during
the lifecycle of a Web page.
The Page Class
7/30/2019 ASP.net Session 01
23/39
Slide 23 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table lists some of the page events:
The Page Class (Contd.)
Event Descript ion
Init It is the first event that occurs when the server executes
an ASP.NET page. It occurs only once in the entire
lifecycle of an ASP.NET page.
Load It is fired after the Init event, when the page is requested
for the first time and whenever the page is reloaded.
This event can be used to initialize the variables and the
state of the controls that are used in the page.
Unload It represents the last event that occurs in the lifecycle of
an ASP.NET page. This event is used to perform final
cleanup work, such as the closing of open database
connections, discarding objects, or closing files.
7/30/2019 ASP.net Session 01
24/39
Slide 24 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
ASP.NET provides the following built-in objects:
Application
Request
Response
Server
Session
HttpContext
The built-in 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.
Intrinsic Objects of ASP.NET
7/30/2019 ASP.net Session 01
25/39
Slide 25 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The Application object:
Provides a reference to an object of theHttpApplicationState class.
Is used to store information that is defined for the entire Web
application.
Has the following properties:
Count: It returns the number of objects in the
HttpApplicationState collection.
Item: It provides access to an object in the HttpApplicationState
collection.
Provides the following methods:public void Lock(): It blocks the access to an
Application variable or object.
public void UnLock(): It releases the lock procured on an
Application variable or object.
Intrinsic Objects of ASP.NET (Contd.)
7/30/2019 ASP.net Session 01
26/39
Slide 26 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The Request object:
Provides a reference to an object of the HttpRequest class.
Enables ASP.NET applications to access information sent by
the client during a Web request.
Provides the following method:
byte[] BinaryRead(int): Helps retrieve the data sent to the
server from the client as part of a post request and stores it in an
array.
Intrinsic Objects of ASP.NET (Contd.)
7/30/2019 ASP.net Session 01
27/39
Slide 27 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table describes some of the properties of theRequest object.
Intrinsic Objects of ASP.NET (Contd.)
Proper ty Descr ipt ion
ApplicationPath Returns the application root path of ASP.NET on the
server. This is a read-only property.
Url Returns a object, which will contain the completeaddress of the requested page. This is a read-only
property.
UserHostAddress Returns the IP host address of the requesting client.
UserLanguage Returns a sorted string array containing the language
preferences of the client.
Browser Returns the HttpBrowserCapabilities object, which
contains information about the browser capability of the
client.
Cookies Returns the HttpCookieCollection object, which provides
access to the cookie variables of the client.
QueryString Provides access to the parameters passed from the
client to the server along with a request.
7/30/2019 ASP.net Session 01
28/39
Slide 28 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The Response object:
Provides a reference to an object of the HttpResponse class.
Enables ASP.NET applications to send information to the
client.
The following table describes some of the properties of theResponse object.
Intrinsic Objects of ASP.NET (Contd.)
Proper ty Descr ipt ion
Buffer Sets the buffer feature of the Response object.
Cache Provides access to the caching policy, such as the
expiration time and privacy settings of the Web page.
ContentType Sets or gets the type of data that is sent to the clientthrough the Response object.
Cookies Sets a cookie to the client browser.
IsClientConnect
edReturns a Boolean value indicating whether the client is
connected or disconnected.
7/30/2019 ASP.net Session 01
29/39
Slide 29 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table describes some of the methods of theResponse object.
Intrinsic Objects of ASP.NET (Contd.)
Method Descr ipt ion
public void Clear() Clears the content in the buffer of the Response
object.
public void Close() Closes the connection to the client.public void End() Sends the buffered data to the client and closes the
connection to the client. The End() method will raise
an Application_EndRequest event.
public void Flush() Sends the data in the buffer to the client but does not
close the connection to the client.
public void Redirect(string) Redirects the new URL specified in the string
parameter to the client.
public void Write(string) Writes a string on the form. It is an overloadedmethod.
Let us see how to display a message on a Web page
7/30/2019 ASP.net Session 01
30/39
Slide 30 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The Server object:
Provides a reference to an object of the HttpServerUtility
class.
Provides methods that can be used to access the methods and
properties of the Web server.
Has the following properties:
MachineName: It returns the name of the server machine.
ScriptTimeout: It helps get and set the request timeout in
seconds for the Web server.
Intrinsic Objects of ASP.NET (Contd.)
7/30/2019 ASP.net Session 01
31/39
Slide 31 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table describes some of the methods of the
Server object.
Intrinsic Objects of ASP.NET (Contd.)
Method Descr ipt ion
public void ClearError() Clears the last error that has occurred on the server.
public object
CreateObject(String)Creates the server instance of a COM object.
public Exception
GetLastError()Returns an Exception object, which represents the last error that
occurred on the server.
public string
MapPath(string path)Returns the physical file path on the Web server that corresponds
to the specified virtual path.
public void
Transfer(string)Terminates the processing of the current page and loads the page
specified in the string format.
7/30/2019 ASP.net Session 01
32/39
Slide 32 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The Session object:
Provides a reference to an object of the HttpSessionState
class.
Enables ASP.NET applications to keep the state of the
application as modified or changed by a client.
Provides access to the session wide cache, which can be used
to store information pertaining to the client.
Has the following properties:
Count: It returns the number of items in the session-state
collection.
SessionID: It returns the unique session ID assigned for asession.
Timeout: It enables to get and set the period in minutes that is
allowed between requests by the client before the session-state
provider terminates the session.
Intrinsic Objects of ASP.NET (Contd.)
7/30/2019 ASP.net Session 01
33/39
Slide 33 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
Provides the following methods:
public void Add(string name, object value): Adds a
new item to the session-state collection.
public void Clear(): Clears all the values stored in the
session-state collection.
public void Remove(string name): Deletes an item from
the session-state collection.
Intrinsic Objects of ASP.NET (Contd.)
7/30/2019 ASP.net Session 01
34/39
Slide 34 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
A new HttpContext object is created for every request
sent to an ASP.NET application.
The object is created at the beginning of a request and
destroyed when the request is completed.
The HttpContext object holds current request-specific
information such as Request, Response, Server, and
Session.
Intrinsic Objects of ASP.NET (Contd.)
l i b li i i S
7/30/2019 ASP.net Session 01
35/39
Slide 35 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table describes some of the properties of theHttpContext object.
Intrinsic Objects of ASP.NET (Contd.)
Proper ty Descr ipt ion
AllErrors Gets an array of errors that are accumulated while processing
an HTTP request.
ApplicationInstance Gets or sets the HttpApplication object for the current HTTP
request.
Timestamp Gets the initial timestamp of the current HTTP request.
Users Gets or sets security information for the current HTTP request.
D l i W b A li ti U i ASP NET
7/30/2019 ASP.net Session 01
36/39
Slide 36 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
The following table describes some of the methods of theHttpContext object.
Intrinsic Objects of ASP.NET (Contd.)
Method Descript ion
public void
AddError(Exception
errorInfo)
Adds an exception to the exception collection for the
current HTTP request.
public void ClearError() Clears all errors for the current HTTP request.
public object
GetConfig(string name)Returns requested configuration information for the
current HTTP request.
D l i W b A li ti U i ASP NET
7/30/2019 ASP.net Session 01
37/39
Slide 37 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
In this session, you learned that:
Web pages can be of the following types:
Static Web page
Dynamic Web page
Server-side scripting provides users with dynamic content that
is based on the information stored at a remote location.
Client-side scripting enables you to develop Web pages that
can dynamically respond to user input without having to
interact with a Web server.
An ASP.NET application has the following elements:
Web Form pagesConfiguration files
XML Web service files
Summary
D l i W b A li ti U i ASP NET
7/30/2019 ASP.net Session 01
38/39
Slide 38 of 39Ver. 1.0
Developing Web Applications Using ASP.NET
ASP.NET has the following advanced features that help
develop robust Web applications:
Compiled code
Enriched tool support
Power and flexibility
SimplicityManageability
Scalability
Security
Extensibility
A Web form is made up of the following two components:
The visual portion
The programming logic
Summary (Contd.)
D l i W b A li ti U i ASP NET
7/30/2019 ASP.net Session 01
39/39
Developing Web Applications Using ASP.NET
ASP.NET provides two models for managing the visual
elements and code:
Single-file page model
Code-behind page model
ASP.NET provides the following built-in objects:
Application
Request
Response
Server
Session
HttpContext
Summary (Contd.)