Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

UNIT-1 Introduction to ASP.

NET

1.1 What is ASP.NET


 ASP.NET is a Web application framework developed by Microsoft to build
dynamic data driven Web applications and Web services.
 ASP.NET is a subset of .NET framework.
 In simple terms a framework is a collection of classes.
 ASP.NET is the successor to classic ASP (Active Server Pages).
 What other technologies can be used to build web applications
1. PHP
2. Java
3. CGI
4. Ruby on Rails
5. Perl
What is a Web Application?
 A web application is an application that is accessed by users using a web
browser. Examples of web browsers include
1. Microsoft Internet Explorer
2. Google Chrome
3. Mozilla FireFox
4. Apple Safari
5. Netscape Navigator
What are the advantages of Web applications?
1. Web Applications just need to be installed only on the web server, where as
desktop applications need to be installed on every computer, where you want to
access them.
2. Maintenance, support and patches are easier to provide.
3. Only a browser is required on the client machine to access a web application.
4. Accessible from anywhere provided there is internet.
5. Cross Platform
How Web applications work?
 Web applications work on client/server architecture
 On the client all you need is a browser, that can understand HTML
 On the server side, the Web application runs under Microsoft Internet
Information Services (IIS)

Page 1
UNIT-1 Introduction to ASP.NET

 When the client enters the URL of the web application in the browser, and
submits the request the web server which hosts the web application
receives therequest.
 The request is then processed by the application.
 The application generates, the HTML and hands it over to the IIS (web
server).
 Finally, IIS sends the generated HTML to the client, who made the initial
request.
 The client browser will interpret the HTML and displays the user interface.
 All this communication happens over the internet using HTTP protocol.
 HTTP stands for Hyper Text Transfer Protocol.
 A protocol is a set of rules that govern how two or more items communicate.
UNIT-1 Introduction to ASP.NET

1.2 .NET Framework Architecture:


Introduction:
.Net Framework is an essential component of the windows operating system
and it helps to create applications by integrating different programming
languages, such as Visual C#, Visual Basic, J#, Visual C++, and so on.

A .NET Framework having two main components:


1. Common Language Runtime (CLR)
2. Base Class Library (BCL) / Framework Class Library (FCL)
UNIT-1 Introduction to ASP.NET

1. Common Language Runtime (CLR):


 It is the execution engine for .NET Framework applications.
 It is the runtime engine provided by the .NET framework.
 It serves as the interface between .NET applications and the operating
system.
 It provides an infrastructure for running programs and allows them to
communicate with other parts of the .NET framework.
 It provides number of services, including the following:
1. Code loading & execution 4. Access to metadata
2. Memory management 5. Access security
3. Exception handling 6. Conversion of IL into native code
 Language Runtime (CLR) manages the execution of code compiled for the
.NET platform The Common
The CLR has two features:
1. Its specification has been opened up so that it can be ported to non-
Windows platforms.
2. Any number of different languages can be used to manipulate the .NET
framework classes, and the CLR will support them.
Common Language Runtime (CLR) Compilation:

 As a developer we can write code in the language of our choice.


 Our code is compiled into an Assembly, which is represented as a DLL or
EXE.
UNIT-1 Introduction to ASP.NET

2. Framework Class Library (FCL):


 It is also known as a base class library. The FCL is a collection of over
7000 reusable classes, interfaces, and value types that enable .NET
applications to :
o read and write files, o display a graphical user interface,
o access databases, o draw graphics,
o process XML, o Use Web services, etc.
 The .Net Framework class library (FCL) organized in a hierarchical tree
structure and it is divided into Namespaces. Namespaces is a logical
grouping of classes having similar functionality.
 Framework class library (FCL) provides the consistent base types that are
used across all .NET enabled languages. The Classes are accessed by
namespaces, which reside within Assemblies.
 System is the root of namespace.
Example: System.IO
System.DATA

3. Common Type System (CTS):


 CTS allow programs written in different programming to easily share
information.
 A class written in C# should be equivalent to a class written in VB.NET.
 Languages must agree on the meanings of these concepts before they can
integrate with one and other.
 CLS forms a subset of Common type system this implies that all the
rules that for apply to Common type system apply to common language
specification.
 It defines rules that a programming language must follow to ensure that
objects written in different programming languages can interact which
each other.
 Common type system provide cross language integration.
UNIT-1 Introduction to ASP.NET

 The common type system supports two general categories of types:


a) Value Type
b) Reference Type
4. Common Language Specification (CLS)
The CLS is a common platform that integrates code and components from
multiple .NET programming languages. In other words, a .NET application can
be written in multiple programming languages with no extra work by the
developer.
 CLS includes basic Language features needed by almost all the
application
 It serves as a guide for Library Writers and Compiler Writer.
 The Common Language Specification is a subset of the common type
system (CTS).
 The common language specification is also important to application to
who are writing codes that will be used by other developers.
 CLS a series of basic rules that are required for language integration.
Microsoft Intermediate Language - MSIL:
 MSIL stands for Microsoft Intermediate Language.
 We can call it as Intermediate Language (IL) or Common Intermediate
Language (CIL).
 During the compile time, the compiler convert the source code into
Microsoft Intermediate Language (MSIL).
 Microsoft Intermediate Language (MSIL) is a CPU-independent set of
instructions that can be efficiently converted to the native code.
 During the runtime the Common Language Runtime (CLR)'s Just in
Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL)
code into native code to the Operating System.
UNIT-1 Introduction to ASP.NET
 When a compiler produces Microsoft Intermediate Language (MSIL), italso
produces Metadata.
 The Microsoft Intermediate Language (MSIL) and Metadata are containedin a
portable executable (PE) file
 Microsoft Intermediate Language (MSIL) includes:
 instructions for loading, storing, initializing, and calling methods on
objects,
 as well as instructions for arithmetic and logical operations, control
flow, direct memory access, exception handling, and other operations

1.3 Compile Code:


Compiled code is a set of files that must be linked together and with one master
list of steps in order for it to run as a program.
This is opposed to a interpreted code like web scripts, host server scripts and
BASIC that are run one line at a time.
Another program called a compiler is designed to maximize the efficiency and
speed of the program so that it runs faster than an interpreted version of the same
program. A compiled program outputs an EXE or DLL file.
A compiler also checks the code for errors. It will work up and down the code to
scan for anything that will crash the program or is syntactically wrong. A program
with untested code can do anything to your computer including very bad things if
not contained within its space.
Lots of time is spent by a programmer to get an error free compiled EXE file. (And
even then you can get infinite loops, etc.)
In the end the point of compiling is to create a most efficient, compact EXE file for
optimal running of the program.

1.3.1 Code behind And Inline Code:


1. Single -File page model(Inline code).
2. Code-behind page model.

1)Single -File page model:- In Single -File page model,the HTML markup and
functionality of a web application are implemented in same File. In single -File coding
approach, developers write code directly in the.aspx page of the application.A major
drawback of the single-File code model is that writing code in a single file, so it is very
difficult to read and manage the webpages.

Example:-
See code (Design) .aspx
File:-Inline Coding model
UNIT-1 Introduction to ASP.NET
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Button1_Click(object sender,
System.EventArgs e) {Label1.Text = "Your Name is : "
+
TextBox1.Text.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Asp.Net Inline Coding Model Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1"
runat="server" Font-Size="Larger"
ForeColor="Crimson"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Enter
your name:"
AssociatedControlID="TextBox1"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>

2)Code -Behind Page Model:-

In Code -Behind Page Model,these are two separate files,Default.aspx and


Default.aspx.cs These two files are linked together to run the web application.All
the different version of ASP.NET Support this model, by default.Both the files are
combined togather during compilation to implement the complete application. In
this model, you need to maintain separate code files for each web page, one file
stores the code to implement the functionality of web page written is some
programming language (c#, vb) and other file stores the HTML Markup of the
web application.
UNIT-1 Introduction to ASP.NET
Example:-

See the Code for Default2.aspx (Design) view:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"


Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0


Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Enter your name: "></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>

The .aspx.cs page will contain the business logic (server side code):

using System;
using System.Configuration;using System.Data;
using System.Linq;using System.Web;
using
System.Web.Securit
y;using
System.Web.UI;
using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
UNIT-1 Introduction to ASP.NET
{
Response.Write("Welcome to our website");
}
}

Code Behind separates user interface from actual programming. This means graphic
designers would look for only design matters while programmers work their way.
Each specialized person work in their irrespective area and doesn't disturb each
other to perform optimized work.

Well there is no performance difference between inline code and code-behind model.

Inline / Single Code Behind


The logic is in <script> blocks in the same .aspx The HTML and controls are in the .aspx file,
file that contains the HTML and controls. and
the logic is in a separate .aspx.cs or .aspx.vb file.
When the source code is deployed along with All project class files (without the .aspx file )
theWeb Forms page, since it is physically in are compiled into a .dll file, which are
the deployed to the server without any source
.aspx file. Though, we are not able to see the code. When a
code,only the results are rendered when the request for the page is received, then an
page runs. instance of the project .dll file is created and
executed.
The .aspx file derives from the Page class. The code for the page is compiled into a
separateclass from which the .aspx file
derives.
When we write inline code we write code in In code-behind approach two separate files
the same page with Html code between arecreated for Html and code.
scripting tags.

1.4 The Common Language Runtime:

Architecture of Common Language Runtime (CLR):


The Common Language Runtime in the .NET Framework is the Virtual Machine component that
handles program execution for various languages such as C#, F#, Visual Basic .NET, etc. The
managed execution environment is provided by giving various services such as memory
management, security handling, exception handling, garbage collection, thread management, etc.
The Common Language Runtime implements the VES (Virtual Execution System) which is a run
time system that provides a managed code execution environment. The VES is defined in Microsoft’s
implementation of the CLI (Common Language Infrastructure).
UNIT-1 Introduction to ASP.NET

There are multiple components in the architecture of Common Language Runtime. Details about
these are given as follows:
 Base Class Library Support: The Common Language Runtime provides support for the base class
library. The BCL contains multiple libraries that provide various features such
as Collections, I/O, XML, DataType definitions, etc. for the multiple .NET programming languages.
 Thread Support: The CLR provides thread support for managing the parallel execution of
multiple threads. The System.Threading class is used as the base class for this.
 COM Marshaller: Communication with the COM (Component Object Model) component in the
.NET application is provided using the COM marshaller. This provides the COM interoperability
support.
 Type Checker: Type safety is provided by the type checker by using the Common Type System
(CTS) and the Common Language Specification (CLS) that are provided in the CLR to verify the
types that are used in an application.
 Exception Manager: The exception manager in the CLR handles the exceptions regardless of
the .NET Language that created them. For a particular application, the catch block of the
exceptions are executed in case they occur and if there is no catch block then the application is
terminated.
 Security Engine: The security engine in the CLR handles the security permissions at various
levels such as the code level, folder level, and machine level. This is done using the various tools
that are provided in the .NET framework.
 Debug Engine: An application can be debugged during the run-time using the debug engine.
There are various ICorDebug interfaces that are used to track the managed code of the application
that is being debugged.
 JIT Compiler: The JIT compiler in the CLR converts the Microsoft Intermediate Language (MSIL)
into the machine code that is specific to the computer environment that the JIT compiler runs on.
The compiled MSIL is stored so that it is available for subsequent calls if required.
 Code Manager: The code manager in CLR manages the code developed in the .NET framework i.e.
the managed code. The managed code is converted to intermediate language by a language-
specific compiler and then the intermediate language is converted into the machine code by the
Just-In-Time (JIT) compiler.
UNIT-1 Introduction to ASP.NET
 Garbage Collector: Automatic memory management is made possible using the garbage collector
in CLR. The garbage collector automatically releases the memory space after it is no longer
required so that it can be reallocated.
 CLR Loader: Various modules, resources, assemblies, etc. are loaded by the CLR loader. Also, this
loader loads the modules on demand if they are actually required so that the program
initialization time is faster and the resources consumed are lesser.

1.5 Object Oriented Concepts:


Following are the OOPs Concepts:
1. Class
2. Objects
3. Data Abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic Binding
8. Message Passing
1. Class
 A class is a data type that has been defined by the user.
 A class is similar to an object's blueprint.
 It is made up of data members and member functions that may be accessed and used by
instantiating the class. It indicates the collection of attributes or methods shared by all
objects of the same type.
 Examples: The Class Car defines methods like StartEngine and Accelerate, as well as
characteristics like color and model.
2. Objects
 It is a fundamental unit of Object-Oriented Programming that represents real-world entities.
 An Object is a subset of a Class.
 When a class is defined, no memory is allocated; nevertheless, memory is allocated when the
class is instantiated (i.e. an object is formed).
 An object has a name, a state, and a behavior.
 Each object contains data as well as code for manipulating the data.
 Objects can interact without knowing the specifics of each other's data or code; all that is
required is knowledge of the type of message accepted and the type of response given by the
objects.
 Examples: Object myCar is an instance of the Car class with its own color, model, and engine
state.
3. Data Abstraction
 The method of hiding an object's internal characteristics from the outside world disclosing
only necessary functions and information.
 This reduces programming complexity while also protecting data integrity by preventing
unauthorized access or tampering.
 For example, a BankAccount class may hide its internal balance while providing deposit,
withdrawal, and checking balance methods.
4. Encapsulation
 Data as well as the functions that operate on that data are combined into a single unit
(class/object).
UNIT-1 Introduction to ASP.NET
 This improves data security, modularity, & reuse.
 management methods
Example: A File class encapsulates data (file content) as well as file management
(read, write, close).

Inheritance
 Inheritance is a key component of OOP (Object-Oriented
(Object Programming).
 Inheritance
itance refers to a class's capacity to derive features and traits from another class.
 We inherit properties from other classes when we write a class.
 So, when we build a class, we don't have to repeat all of the properties and functions because
they can be inherited from another class that has them.
 Inheritance allows the user to reuse code as much as possible, reducing repetition.
 Examples: Class ElectricCar inherits from Car but adds specific methods like chargeBattery.

Polymorphism
 The ability of an entity
tity (object or function) to change its form or behavior based on the
situation.
 Treating objects of different types similarly provides flexibility and simplifies code.
 Examples: By modifying its behavior, a print() function can handle various data types llike
strings, integers, and objects.

Dynamic Binding
 At runtime, the process of binding a function call to the correct implementation based on the
type of the object at that time.
 This allows for more flexible behavior and late-function
late binding.
 Example: Depending on the object calling it at runtime, the eat() function in Animal may have
different implementations for Cat and Dog.
Message Passing
UNIT-1 Introduction to ASP.NET
 In object-oriented programming, the communication method between objects.
 Messages (function calls) are sent between objects with data as arguments, activating certain
behaviors.
 This encourages modularity and decoupling, resulting in more manageable and reusable
code.
 Example: To initiate the window's close function, a Button object clicks on a Window object.

1.6 Event Driven Programming:

Event-driven programming (EDP) is a programming paradigm where external events determine the
flow of program execution. These events come in different shapes: user actions (e.g., button clicks,
keyboard inputs), system events (like a finished file download), messages from other programs,
sensor outputs, etc.

ASP.Net Page Execution


Following are the different stages of an ASP.Net page:

 Page request: This stage occurs before the lifecycle begins. When a page is requested
by the user, ASP.NET parses and compiles that page.
 Starting of page life cycle: at this stage, the Request and Response objects are set. It
also determines the Request type. 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: In this stage, each control's UniqueID property is set. Master page
and themes are also applied to the page.
 Page load: During this phase, if page request is postback, control properties are
loaded with information.
 Validation: Validate method of the validation control is called and if it runs
successfully, the IsValidproperty of the page is set to true.
 Postback event handling: If the request is a postback (old request), the related event
handler is called. After that the Validate method of all validator controls is called. 
 Page rendering: At this stage, view state for the page and all controls are saved. The
page calls the Render method for each control and the output of rendering is written to
the OutputStream class of the Page's Response property. 
 Unload: When everything is over the unload state is performed. At this stage the
requested page has been fully rendered and is ready to terminate. All properties like
Request and Response are unloaded and cleanup is performed. 
UNIT-1 Introduction to ASP.NET

ASP.NET Page Life Cycle Events


 At each stage of the page life cycle, the page raises some events, which could be coded. An event
handler is basically a function or subroutine, bound to the event, using declarative attributes such as
Onclick or handle.

 Following are the page life cycle events:

1. PreInit –
o PreInit is the first event in page life cycle. It checks the IsPostBack property and determines
whether the page is a postback.
o It sets the themes and master pages, creates dynamic controls, and gets and sets profile property
values.
o This event can be handled by overloading the OnPreInit method or creating a Page_PreInit
handler.
2. Init –
o 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.
3. InitComplete –
o InitComplete event allows tracking of view state. All the controls turn on view-state tracking.
4. LoadViewState –
o LoadViewState event allows loading view state information into the controls.
5. LoadPostData –
o During this phase, the contents of all the input fields are defined with the <form> tag are
processed.

6. PreLoad –
o PreLoad occurs before the post back data is loaded in the controls.
o This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad
handler.
7. Load –
o The Load event is raised for the page first and then recursively for all child controls.
o The controls in the control tree are created. This event can be handled by overloading the OnLoad
method or creating a Page_Load handler.
8. LoadComplete –
o The loading process is completed, control event handlers are run, and page validation takes
place.
o This event can be handled by overloading the OnLoadComplete method or creating a
Page_LoadComplete handler
9. PreRender –
UNIT-1 Introduction to ASP.NET
o The PreRender event occurs just before the output is rendered. By handling this event, pages and
controls can perform any updates before the output is rendered.
10. PreRenderComplete –
o As the PreRender event is recursively fired for all child controls, this event ensures the completion
of the pre-rendering phase.
11. SaveStateComplete –
o State of control on the page is saved. Personalization, control state and view state information is
saved.
o The HTML markup is generated. This stage can be handled by overriding the Render method or
creating a Page_Render handler.
12. UnLoad –
o 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.
o Final cleanup is done and all resources and references, such as database connections, are freed.
o This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad
handler.

You might also like