You are on page 1of 42

ASP.

NET Web Forms

ASP.NET Web Forms

Main Menu 1 of 42
ASP.NET Web Forms

Objective
 At the end of this chapter, you will be able to
understand what are ASP.NET Web Forms, how
Visual Studio.NET creates these.
 You will an understanding of how and why
Microsoft has separated the user interface resources
from the business logic.
 You will also get in-depth knowledge of the Page
Class, Page, the sequence of events and setting the
Page Directives.

Main Menu 2 of 42
ASP.NET Web Forms

Scope
 What are Web Forms?
 ASP.NET Frame work
 Visual Studio ASP.NET Web Applications
• Where Does Visual Studio Fit In?
• What’s in Your Project?
 Web Forms Code Model
 Page Class

Main Menu 3 of 42
ASP.NET Web Forms

Scope
• Location
• Properties of Page class
• Methods
• Events of Page class
 Web Forms page life cycle
• Page_Init Event
• Page_Load Event
• Other Events
• Page_Unload Event

Main Menu 4 of 42
ASP.NET Web Forms

Scope
 ASP.NET Page directives
• Page
• Control
• Import
• Register
• Assembly
• OutputCache
• Implements
• Reference

Main Menu 5 of 42
ASP.NET Web Forms

What are Web Forms ?


 ASP.NET web forms are nothing but text files,
saved with an extension of .aspx.
 The web forms can be created in the following three
ways:
• Notepad
• Front Page
• VB.NET web Forms

Main Menu 6 of 42
ASP.NET Web Forms

ASP.NET Frame Work


 Web Forms has the following features : -
• Browser-independent applications.
• Event-based programming model.
• Abstract, intuitive, consistent object model.
• State management.
• Scalable server performance.

Main Menu 7 of 42
ASP.NET Web Forms

Visual Studio ASP.NET Web


Applications
 ASP.NET Web applications run on a Web server
configured with Microsoft IIS.
 You do not need to learn working directly with IIS
or configuring it. It can all be programmed using
ASP.NET classes.
 Visual Studio handles all the file management tasks
such as creating IIS applications when needed and
providing ways for you to even deploy your Web
applications to IIS.
Main Menu 8 of 42
ASP.NET Web Forms

Where Does Visual Studio Fit In?


 The advantage of using Visual Studio is that it
provides tools that make web Application
development much faster, easier, universally
readable and much more robust.
 These tools include:
• Visual designers
• Code-aware editors
• Integrated compilation and debugging
• Project management

Main Menu 9 of 42
ASP.NET Web Forms

What’s in Your Project ?


 The ASP.NET Web Application project template
automatically adds the essential project references
and files to use as a starting point for your
application.
 When you create a Web project in Visual Studio,
Web Forms designer is opened.
 The Web Forms designer is the design surface for
your pages.

Main Menu 10 of 42
ASP.NET Web Forms

What’s in Your Project ?


 You can use the design view (which is open in the
image ) to design the lay out your page and its
elements, HTML view to view the raw HTML
generated for your page, and the code-behind view
to look at the code file associated with your page.

Main Menu 11 of 42
ASP.NET Web Forms

What’s in Your Project ?


 When you create a Web project, Visual Studio
constructs a Web application directory structure on
the target Web server that you have configured
(Your IIS in this case),
 And a project structure on your local computer.

Main Menu 12 of 42
ASP.NET Web Forms

Web Forms Code Model


 Separation of user interface resources from business
logic:
• Web Forms divide the Web applications into two pieces:
• User Interface: .
• It is the visual part, which interacts with the user.
• This part will be dealing with the user.
• Business Logic:
• User interface logic for the Web form consists of code that you
create to interact with the form in a separate file also known as
"code-behind" file.

Main Menu 13 of 42
ASP.NET Web Forms

Page Class
 When the web form page is compiled, ASP.NET
generates a new class and then compiles the new
class.
 This new class is derived from the ASP.NET Page
class, but is extended with controls, your code, and
the static HTML text in the. aspx file.
 In ASP.NET entire Web Form is an executable
program whose output is HTML.

Main Menu 14 of 42
ASP.NET Web Forms

Page Class
 The page goes through a series of processing stages
analogous with those of other components —
initialize, process, and dispose.
 The page class performs these stages each time the
page is called; that is, the page is initialized,
processed, and disposed every time a call is made to
the server.
 For efficiency the information required to recreate
the page is cached, but this is independent of its
lifecycle.

Main Menu 15 of 42
ASP.NET Web Forms

Location

 Page class Defines the properties, methods, and


events common to all pages that are processed on the
server by the Web Forms page framework.
Main Menu 16 of 42
ASP.NET Web Forms

Properties of Page Class


 Examples of Page Class Properties:
• Write the following code in the page_Load event
Response.Write(Page.Application)
Response.Write("<br> Is PostBack = " &
Page.IsPostBack)

Main Menu 17 of 42
ASP.NET Web Forms

Properties of Page Class


 The output would be:

Main Menu 18 of 42
ASP.NET Web Forms

Methods
 Write the following code in the Page_Load event to
see the absolute URL of your web form
Response.Write(Page.ResolveUrl
("WebForm1.aspx"))

Main Menu 19 of 42
ASP.NET Web Forms

Methods
 Output would be:

Main Menu 20 of 42
ASP.NET Web Forms

Public Instance Events of Page


class
 With all these properties, methods and events Web
Forms offer great functionalities to the programmer.
 As every web form ultimately gets compiled in to
class, which is a sub class of Page class, hence this
new compiled class can access all the methods of its
parent class.

Main Menu 21 of 42
ASP.NET Web Forms

Web Forms Page Life Cycle


 Life cycle of any process consists of various stages
involved in its life.
 The life cycle for a Web Forms page is, similar to
that of any Web process that runs on the server.
 In Web Forms, most user actions such as clicking a
button result in a round trip.
 For that reason, the events available in Web Forms
controls are limited.
 Most controls expose a click event.

Main Menu 22 of 42
ASP.NET Web Forms

Web Forms Page Life Cycle


 Life cycle of a Web Form page can be divided in
three main events: -
• Page_Init Event
• Page_Load Event
• Other Events
• Page_Unload Event

Main Menu 23 of 42
ASP.NET Web Forms

Page_Init Event
 This is the first event in page life cycle. This event is
fired whenever the page is initialized.
 Example:
 Enter the following code in the page_init event
Response.write(“<BR> In Page init
event” )
 And this would be the first line on your web page.

Main Menu 24 of 42
ASP.NET Web Forms

Page_Load Event
 This is event is fired when the page is loaded.
 Difference between init and loading event is that the
controls are fully loaded in the load event only.
 Enter the following code in the Page_Load event
Response.Write("<BR> In page load
Page Loaded at : " &
DateTime.Now)
 And this would be the second line on your web page.

Main Menu 25 of 42
ASP.NET Web Forms

Control Events
 These are the events, which are fired after the
Page_Load event.
 If the page was called in response to a form event,
the corresponding event-handling method in the
page is called during this stage.
 Events like button_pressed;
mouse_clicked etc. can be put here.

Main Menu 26 of 42
ASP.NET Web Forms

Page_Unload Event
 This event is called, when the page has performed
all necessary actions and is ready to be discarded.
 All the coding, necessary before cleaning up, is
placed here. Few of such tasks could be closing files,
closing database connections, or discarding objects.
 It is always better to close the connections, which
has been opened in your program.

Main Menu 27 of 42
ASP.NET Web Forms

ASP.NET Page Directives


 ASP.NET supports few directives, which are used to
specify optional settings used by the page compiler
when processing ASP.NET files.
 These directive can be placed anywhere in the page
but conventionally these are placed at the top of the
file.
 Each directive can contain one or more
attribute/value pair’s specific to that directive.
Syntax of using directive as follows: -
<%@ directive attribute=value
[attribute=value …] %>
Main Menu 28 of 42
ASP.NET Web Forms

@Page
 The @ Page directive defines page-specific
attributes used by the ASP.NET page parser and
compiler.
<%@ Page attribute=value
[attribute=value …] %>
 You can only include one @ Page directive per
.aspx file.
 To define multiple attributes, use a space-separated
list, with no space around the equals sign, as in
TRACE="True".

Main Menu 29 of 42
ASP.NET Web Forms

@Control
 The @ Control directive defines user control-
specific attributes used by the ASP.NET page
compiler.
 This directive can only be used with user controls.
<%@ Control attribute=value
[attribute=value … ]%>
 The @ Control directive supports the same attributes
as the @ Page directive, except the AspCompat and
Trace attributes.
 You can only include one @ Control directive per
.ascx file.
Main Menu 30 of 42
ASP.NET Web Forms

@Import
 The @ Import directive explicitly imports a
namespace into a page, making all classes and
interfaces of the imported namespace available to
the page.
 The imported namespace can be either part of the
.NET Framework class library or a user-defined
namespace. Syntax is as follows: -
<%@ Import namespace="value" %>
 The @ Import directive cannot have more than one
namespace attribute.

Main Menu 31 of 42
ASP.NET Web Forms

@Register
 The @Register directive associates aliases with
namespaces and class names for concise notation in
custom server control syntax.
 When a new namespace is added to a page you have
to specify to the compiler few things about the
control.
 With this information compiler can recognize the
control.

Main Menu 32 of 42
ASP.NET Web Forms

@Register
 There are two ways to use this tag: -
<%@ Register
tagprefix="tagprefix"
Tagname=”tagname” Src =
“pathname”%>
<%@Register
tagprefix="tagprefix"
Namespace=”namespace”Assembly=
”assembly”%>

Main Menu 33 of 42
ASP.NET Web Forms

@Assembly
 The @Assembly directive declaratively links an
assembly to the current page, making all of the
assembly's classes and interfaces available for use on
the page.
 You can also use this directive to register assemblies
in the configuration file to link assemblies across the
entire application.
<% @Assembly Name="assemblyname" %>
 It is not necessary to include the path of an assembly
in a @ Assembly directive.

Main Menu 34 of 42
ASP.NET Web Forms

@OutputCache
 This directive is used to control the caching of a
page on the server.
 The @OutputCache directive declaratively controls
the output caching policies of a page.

Main Menu 35 of 42
ASP.NET Web Forms

@Implements
 The @implements directive allows you to
implement a .NET interface in your page.
 When an interface is implemented the page supports
the defined properties, events and methods of the
specified interface.
 Using the following syntax this directive is used
<%@ Implements Interface=“System.
Web.UI.IPostBackEventHandler” %>

Main Menu 36 of 42
ASP.NET Web Forms

@Reference
 It declaratively indicates that another user control or
page source file should be dynamically compiled and
linked against the current page.
<%@ Reference page control=
“pathtofile” %>
 Attributes
• Page
• Control

Main Menu 37 of 42
ASP.NET Web Forms

Summary
 Key points covered in this chapter are:
• Web forms are used to share information between browser
and server.
• Web forms are text files only, saved with an extension of
aspx.
• Visual Studio.NET provides an easy to use GUI for
developing web applications.
• There is no need to create a virtual directory every time
we create a web application in Visual Studio.NET

Main Menu 38 of 42
ASP.NET Web Forms

Summary
• Visual studio provides different views to write different
types of codes eg. HTML view, Code View, design view
• Visual studio generates a number of files with different
functionalities in the web project.
• Web forms Code Model provides separation of the html
code from programming logic.
• Page is represented as a class in .NET framework.
• Page has its own class hierarchy, properties, methods and
events.
• Web forms Page Life Cycle is based on events.

Main Menu 39 of 42
ASP.NET Web Forms

Summary
• Page_Init Event is the first event fired as soon as the page
is loaded.
• Other Events are events fired by the program depending
on the page. These events are defined by the developer
e.g. button click event.
• Page_Unload Event is the last event to write the clean up
code.
• ASP.NET Page directives are used to declare various
directives on the page namely Page, Control, Import,
Register, Assembly and OutputCache. These give our web
page a default behavior.

Main Menu 40 of 42
ASP.NET Web Forms

Self Assessment
 Fill in the blanks:
1. ______________event is called, when the page has
performed all necessary actions and is ready to be
discarded.
2. _____________ is a WYSIWYG editing tool for
designing and programming our Web Forms.
3. ASP.NET web forms are nothing but text files, saved
with an extension of ________.
4. Code behind file has a ___________ extension.

Main Menu 41 of 42
ASP.NET Web Forms

Self Assessment
 State True/False :
1. Web forms can be created using visual studio.net only.
2. Web forms are used to share information between
browser and server.
3. Web forms are windows based forms.
4. Page directives are used to control information with in
the aspx page.

Main Menu 42 of 42

You might also like