Unit 1
Unit 1
ASP.NET
ASP.NET works on top of the HTTP protocol, and uses the HTTP commands
and policies to set a browser-to-server bilateral communication and
cooperation.
C#
Visual Basic.Net
Jscript
J#
All client side user activities are forwarded to the server for stateful
processing. The server processes the output of the client actions and triggers
the reactions.
Now, HTTP is a stateless protocol. ASP.NET framework helps in storing the
information regarding the state of the application, which consists of:
Page state
Session state
The page state is the state of the client, i.e., the content of various input fields
in the web form. The session state is the collective information obtained from
various pages the user visited and worked with, i.e., the overall session state.
To clear the concept, let us take an example of a shopping cart.
User adds items to a shopping cart. Items are selected from a page, say the
items page, and the total collected items and price are shown on a different
page, say the cart page. Only HTTP cannot keep track of all the information
coming from various pages. ASP.NET session state and server side
infrastructure keeps track of the information collected globally over a session.
The ASP.NET runtime carries the page state to and from the server across
page requests while generating ASP.NET runtime codes, and incorporates the
state of the server side components in hidden fields.
This way, the server becomes aware of the overall application state and
operates in a two-tiered connected way.
ASP.NET is a technology, which works on the .Net framework that contains all
web-related functionalities. The .Net framework is made of an object-oriented
hierarchy. An ASP.NET web application is made of pages. When a user
requests an ASP.NET page, the IIS delegates the processing of the page to the
ASP.NET runtime system.
The ASP.NET runtime transforms the .aspx page into an instance of a class,
which inherits from the base class page of the .Net framework. Therefore, each
ASP.NET page is an object and all its components i.e., the server-side controls
are also objects.
It contains the specifications for the .Net supported languages and implementation of
language integration.
It provides guidelines for declaring, using, and managing types at runtime, and cross-
language communication.
(8) ADO.NET
It is the technology used for working with data and databases. It provides access to
data sources like SQL server, OLE DB, XML etc. The ADO.NET allows connection to
data sources for retrieving, manipulating, and updating data.
(13) LINQ
It imparts data querying capabilities to .Net languages using a syntax which is similar
to the tradition query language SQL.
ASP.NET - Environment Setup
The key development tool for building ASP.NET applications and front ends is
Visual Studio. In this tutorial, we work with Visual Studio 2008.
Installation.
Microsoft provides a free version of visual studio which also contains SQL
Server and it can be downloaded from www..visualstudio..com.
Step 1 − Once downloading is complete, run the installer. The following dialog
will be displayed.
Step 2 − Click on the Install button and it will start the installation process.
Step 3 − Once the installation process is completed successfully, you will see
the following dialog. Close this dialog and restart your computer if required.
Step 4 − Open Visual Studio from start Menu which will open the following
dialog. It will be a while for the first time for preparation.
Step 5 − Once all is done you will see the main window of Visual studio.
Let’s create a new project from File → New → Project
When you start a new web site, ASP.NET provides the starting folders and files
for the site, including two files for the first web form of the site.
The file named Default.aspx contains the HTML and asp code that defines the
form, and the file named Default.aspx.cs (for C# coding) or the file named
Default.aspx.vb (for VB coding) contains the code in the language you have
chosen and this code is responsible for the actions performed on a form.
The primary window in the Visual Studio IDE is the Web Forms Designer
window. Other supporting windows are the Toolbox, the Solution Explorer,
and the Properties window. You use the designer to design a web form, to add
code to the control on the form so that the form works according to your need,
you use the code editor.
To change the Web Forms Designer from one view to another, click on
the Design or source button.
To close a window, click on the close button on the upper right corner
and to redisplay, select it from the View menu.
To hide a window, click on its Auto Hide button. The window then
changes into a tab. To display again, click the Auto Hide button again.
To change the size of a window, just drag it.
Solutions may contain one or more projects. A project contains content files,
source files, and other files like data sources and image files. Generally, the
contents of a project are compiled into an assembly as an executable file (.exe)
or a dynamic link library (.dll) file.
Selecting Start
Selecting Start Without Debugging from the Debug menu,
pressing F5
Ctrl-F5
The program is built meaning, the .exe or the .dll files are generated by
selecting a command from the Build menu.
When a page is requested, it is loaded into the server memory, processed, and
sent to the browser. Then it is unloaded from the memory. At each of these
steps, methods and events are available, which could be overridden according
to the need of the application. In other words, you can write your own code to
override the default code.
The Page class creates a hierarchical tree of all the controls on the page. All
the components on the page, except the directives, are part of this control tree.
You can see the control tree by adding trace= "true" to the page directive. We
will cover page directives and tracing under 'directives' and 'event handling'.
Initialization
Instantiation of the controls on the page
Restoration and maintenance of the state
Execution of the event handler codes
Page rendering
Understanding the page cycle helps in writing codes for making some specific
thing happen at any stage of the page life cycle. It also helps in writing custom
controls and initializing them at right time, populate their properties with
view-state data and run control behavior code.
Starting of page life cycle - At this stage, the Request and Response
objects are set. If the request is an old request or post back, the
IsPostBack property of the page is set to true. The UICulture property of
the page is also set.
Page initialization - At this stage, the controls on the page are assigned
unique ID by setting the Unique ID property and the themes are applied.
For a new request, postback data is loaded and the control properties
are restored to the view-state values.
Page load - At this stage, control properties are set using the view state
and control state values.
Unload - The rendered page is sent to the client and page properties,
such as Response and Request, are unloaded and all cleanup done.
PreInit - PreInit is the first event in page life cycle. It checks the
IsPostBack property and determines whether the page is a postback. It
sets the themes and master pages, creates dynamic controls, and gets
and sets profile property values. This event can be handled by
overloading the OnPreInit method or creating a Page_PreInit handler.
Init - Init event initializes the control property and the control tree is
built. This event can be handled by overloading the OnInit method or
creating a Page_Init handler.
LoadPostData - During this phase, the contents of all the input fields are
defined with the <form> tag are processed.
PreLoad - PreLoad occurs before the post back data is loaded in the
controls. This event can be handled by overloading the OnPreLoad
method or creating a Page_PreLoad handler.
Load - The Load event is raised for the page first and then recursively
for all child controls. The controls in the control tree are created. This
event can be handled by overloading the OnLoad method or creating a
Page_Load handler.
UnLoad - The UnLoad phase is the last phase of the page life cycle. It
raises the UnLoad event for all controls recursively and lastly for the
page itself. Final cleanup is done and all resources and references, such
as database connections, are freed. This event can be handled by
modifying the OnUnLoad method or creating a Page_UnLoad handler.
ASP.Net Architecture
1. Code Behind Mode – This is the concept of separation of design and code.
By making this separation, it becomes easier to maintain the ASP.Net
application. The general file type of an ASP.Net file is aspx. Assume we
have a web page called MyPage.aspx. There will be another file called
MyPage.aspx.cs which would denote the code part of the page. So Visual
Studio creates separate files for each web page, one for the design part and
the other for the code.
ASP.NET runtime controls the association between a page instance and its state.
An ASP.NET page is an object of the Page or inherited from it.
All the controls on the pages are also objects of the related control class inherited
from a parent Control class. When a page is run, an instance of the object page is
created along with all its content controls.
An ASP.NET page is also a server side file saved with the .aspx extension. It is
modular in nature and can be divided into the following core sections:
Page Directives
Code Section
Page Layout
Page Directives
The page directives set up the environment for the page to run. The @Page
directive defines page-specific attributes used by ASP.NET page parser and
compiler. Page directives specify how the page should be processed, and which
assumptions need to be taken about the page.
Code Section
The code section provides the handlers for the page and control events along with
other functions required. We mentioned that, ASP.NET follows an object model.
Now, these objects raise events when some events take place on the user interface,
like a user clicks a button or moves the cursor. The kind of response these events
need to reciprocate is coded in the event handler functions. The event handlers are
nothing but functions bound to the controls.
The code section or the code behind file provides all these event handler routines,
and other functions used by the developer. The page code could be precompiled
and deployed in the form of a binary assembly.
The server has a subroutine describing what to do when the event is raised; it
is called the event-handler. Therefore, when the event message is transmitted
to the server, it checks whether the Click event has an associated event
handler. If it has, the event handler is executed.
Event Arguments
ASP.NET event handlers generally take two parameters and return void. The
first parameter represents the object raising the event and the second
parameter is event argument.
Handles btnCancel.Click
End Sub
An event can also be coded without Handles clause. Then, the handler must be
named according to the appropriate event attribute of the control.
End Sub
Some events cause the form to be posted back to the server immediately, these are
called the postback events. For example, the click event such as, Button.Click.
Some events are not posted back to the server immediately, these are called non-
postback events.
Default Events
The default event for the Page object is Load event. Similarly, every control has a
default event. For example, default event for the button control is the Click event.
The default event handler could be created in Visual Studio, just by double
clicking the control in design view. The following table shows some of the default
events for common controls:
AdRotator AdCreated
BulletedList Click
Button Click
Calender SelectionChanged
CheckBox CheckedChanged
CheckBoxList SelectedIndexChanged
DataGrid SelectedIndexChanged
DataList SelectedIndexChanged
DropDownList SelectedIndexChanged
HyperLink Click
ImageButton Click
ImageMap Click
LinkButton Click
ListBox SelectedIndexChanged
Menu MenuItemClick
RadioButton CheckedChanged
RadioButtonList SelectedIndexChanged
Example.
This example includes a simple page with a label control and a button control on it.
As the page events such as Page Load, Page_Init, Page_PreRender etc. take place,
it sends a message, which is displayed by the label control. When the button is
clicked, the Button_Click event is raised and that also sends a message to be
displayed on the label.
Create a new website and drag a label control and a button control on it from the
control tool box. Using the properties window, set the IDs of the controls
as .lblmessage and .btnclick. respectively. Set the Text property of the Button
control as 'Click'.
Print Page