Professional Documents
Culture Documents
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
Main Menu 6 of 42
ASP.NET Web Forms
Main Menu 7 of 42
ASP.NET Web Forms
Main Menu 9 of 42
ASP.NET Web Forms
Main Menu 10 of 42
ASP.NET Web Forms
Main Menu 11 of 42
ASP.NET Web Forms
Main Menu 12 of 42
ASP.NET Web Forms
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
Main Menu 17 of 42
ASP.NET Web Forms
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
Main Menu 21 of 42
ASP.NET Web Forms
Main Menu 22 of 42
ASP.NET Web Forms
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
@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