0% found this document useful (0 votes)
20 views60 pages

Asp 200531103252

Uploaded by

anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views60 pages

Asp 200531103252

Uploaded by

anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 60

Introduction to ASP.

NET
What is ASP.NET ?
 ASP.NET: server side technology for creating dynamic web pages using Fully Fledged
programming languages supported by .NET

 ASP.NET is a web development platform, which provides a programming model, a


comprehensive software infrastructure and various services required to build up robust
web applications for PC as well as mobile devices.

 ASP.NET applications are compiled codes, written using the extensible and reusable
components or objects present in .Net framework. These codes can use the entire
hierarchy of classes in .Net framework.

 ASP.NET application codes can be written in any of the following languages:


 C#
 Visual Basic.Net
 Jscript
 J#
Features
 Mobile web device support
 High scalability
 Better language support
 Easier configuration and deployment
 XML based components
 Event driven programming
 Dynamic update of running application
Advantages of ASP.NET
1. ASP.NET drastically reduces the amount of code required to build large
applications.

2. With built-in Windows authentication and per-application configuration, your


applications are safe and secured.

3. It provides better performance by taking advantage of early binding, just-in-


time compilation, native optimization, and caching services right out of the
box.

4. The ASP.NET framework is complemented by a rich toolbox and designer in


the Visual Studio integrated development environment. Drag-and-drop server
controls, and automatic deployment are just a few of the features this
powerful tool provides.

5. Provides simplicity as ASP.NET makes it easy to perform common tasks,


from simple form submission and client authentication to deployment and site
configuration.
6. The source code and HTML are together therefore ASP.NET pages are easy to
maintain and write. Also the source code is executed on the server. This provides
a lot of power and flexibility to the web pages.

7. All the processes are closely monitored and managed by the ASP.NET runtime,
so that if process is dead, a new process can be created in its place, which helps
keep your application constantly available to handle requests.

8. It is purely server-side technology so, ASP.NET code executes on the server


before it is sent to the browser.

9. Being language-independent, it allows you to choose the language that best


applies to your application or partition your application across many languages.

10. ASP.NET makes for easy deployment. There is no need to register components
because the configuration information is built-in.

11. The Web server continuously monitors the pages, components and applications
running on it. If it notices any memory leaks, infinite loops, other illegal
activities, it immediately destroys those activities and restarts itself.

12. Easily works with ADO.NET using data-binding and page formatting features. It
is an application which runs faster and counters large volumes of users without
having performance problems
Difference between ASP and ASP.NET
1. ASP is interpreted, ASP.NET is compiled.
2. Classic ASP uses a technology called ADO to connect and work with
databases. ASP.NET uses the ADO.NET technology
3. ASP has Mixed HTML and coding logic where in asp.net html and
coding part are separated by code behind files.
4. ASP.NET purely object oriented whereas ASP is partially object
oriented.
5. For ASP No in-built support for XML whereas in ASP.NET full XML
Support for easy data exchange.
6. In Asp Error handling is very poor where in asp.net full proof error
handling is possible
7. ASP has no in built validation control. Meaning that validating page is
difficult for developers.Asp.net has in built rich validation set.
8. No built in support for XML in ASP but in asp.net it is.
9. Limited session and application state management in ASP but in asp.net
it is complete.
LIFE CYCLE
ASP.NET life cycle specifies how:
 ASP.NET processes pages to produce dynamic output
 The application and its pages are instantiated and processed
 ASP.NET compiles the pages dynamically
ASP.NET life cycle could be divided into two groups:
 Application Life Cycle
 Page Life Cycle
ASP.NET Application Life Cycle
The application life cycle has the following stages:
1. User makes a request for accessing application resource, a page. Browser

sends this request to the web server.


2. A unified pipeline receives the first request and the following events take
place:
i. An object of the class ApplicationManager is created.
ii. An object of the class HostingEnvironment is created to provide
information regarding the resources.
iii. Top level items in the application are compiled.
3. Response objects are created. The application objects such as
HttpContext, HttpRequest and HttpResponse are created and initialized.
4. An instance of the HttpApplication object is created and assigned to the
request.
5. The request is processed by the HttpApplication class. Different events
are raised by this class for processing the request.
ASP.NET Page Life Cycle
Page life cycle phases are:
 Initialization
 Instantiation of the controls on the page
 Restoration and maintenance of the state
 Execution of the event handler codes
 Page rendering
ASP.NET Page Life Cycle
Following are the different stages of an ASP.NET page:
 Page request
When ASP.NET gets a page request, it decides whether to parse and
compile the page, or there would be a cached version of the page;
accordingly the response is sent.
 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
UniqueID 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.
 Validation
Validate method of the validation control is called and on its successful
execution, the IsValid property of the page is set to true.
 Postback event handling.
If the request is a postback (old request), the related event handler is
invoked.
 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 Response property of Page.
 Unload
The rendered page is sent to the client and page properties, such as
Response and Request, are unloaded and all cleanup done.
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:
 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.
 InitComplete -InitComplete event allows tracking of view state. All the
controls turn on view-state tracking.
 LoadViewState-LoadViewState event allows loading view state information
into the controls.
 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.
 LoadThe 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.
 LoadComplete -The loading process is completed, control event handlers
are run, and page validation takes place. This event can be handled by
overloading the OnLoadComplete method or creating a
Page_LoadComplete handler.
 PreRender-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.
 PreRenderComplete-As the PreRender event is recursively fired for all
child controls, this event ensures the completion of the pre-rendering phase.
 SaveStateComplete-State of control on the page is saved. Personalization,
control state and view state information is saved. The HTML markup is
generated. This stage can be handled by overriding the Render method or
creating a Page_Render 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
 Visual Studio .NET is a developer application used to create ASP.NET
Web applications
 There are two main types of Web resources created with ASP.NET
applications
 WebForms are ASP.NET pages within an ASP.NET application
 Web Services are ASP.NET Web pages that contain publicly exposed
code so that other applications can interact with them.Web Services are
identified with the file extension .asmx
WebForms
The ASP.NET WebForm is separated into two logical areas:
 The HTML template
 A collection of code behind the WebForm
The HTML template
 Contains the design layout, content, and the controls
 Creates the user interface, or presentation layer
 Instructs the browser how to format the Web page
 Is created using a combination of HTML controls, HTML Server
controls, Mobile Controls, and ASP.NET controls
The Code Behind
 Server programs are written in a separate file known as the
code behind the page
 By separating the programming logic and presentation layer,
the application becomes easier to maintain
 Only Server controls can interact with the code behind the page
 Written in any ASP.NET compatible language such as
Visual Basic .NET, C#, Perl, or Java
 Filename is the same as the WebForm filename
 Add a file extension that identifies the language
 Visual Basic .NET use .vb (mypage.aspx.vb)
 C# use .cs (mypage.aspx.cs)
STARTING A NEW WEBSITE
Open Visual Web Developer 2010 and in the file menu select the
new website option from the dropdown menu or on the start page.
When the new website dialogue box appears, select installed
templates and then select visual C# from the dropdown list to set
the programing language. Visual studio comes with two pre-
installed “code-behind” programming languages C# and Visual
Basic.
Select Empty ASP.NET Website from the list in the center. This
will start your project without the default webpage. When starting a
new website, visual studio gives you the option of either creating a
blank website or opening a pre-structured website that has a default
style and master page already applied to it.
CREATING A BASIC WEB PAGE
In the solution explorer, right click the name of the
website (Example:C:\…\…website1) and select Add
New Item from the list.
When the Add New Item display opens, select Web
Form from the installed templates list. Take look at
the installed templates list. These are all of the pre-
installed templates that can be used to create
dynamic web applications.
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.
It allows importing namespaces, loading assemblies,
and registering new controls with custom tag names
and namespace prefixes.
Code Section
The code section provides the handlers for the page
and control events along with other functions
required.
The code section or the code behind file provides all
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.
Page Layout
 The page layout provides the interface of the page. It contains the server
controls, text, inline JavaScript, and HTML tags.
 The following code snippet provides a sample ASP.NET page explaining
Page directives, code section and page layout written in C#:
CONTROLS
 Controls are small building blocks of the graphical user interface, which
include text boxes,buttons, check boxes, list boxes, labels, and numerous
other tools. Using these tools, the users can enter data, make selections and
indicate their preferences.
 Controls are also used for structural jobs, like validation, data access,
security, creating master pages, and data manipulation.
 ASP.NET uses five types of web controls:
• HTML controls
• HTML Server controls
• ASP.NET Server controls
• ASP.NET Ajax Server controls
• User controls and custom controls
 The HTML server controls are basically the standard HTML controls
enhanced to enable server side processing. The HTML controls such as the
header tags, anchor tags, and input elements are not processed by the server
but are sent to the browser for display.
ASP.NET HTML Server Controls
 ASP.NET provides a way to work with HTML Server controls on the
server side; programming with a set of controls collectively is called HTML
Controls.
 These controls are grouped together in the Visual Studio Toolbox in the the
HTML Control tab. The markup of the controls are similar to the HTML
control.
 These controls are basically the original HTML controls but enhanced to
enable server side processing.
 HTML elements in ASP. NET files are, by default, treated as text. To make
these elements programmable, add a runat="server" attribute to the HTML
element. This attribute indicates that the element should be treated as a
server control.
Note:
 All HTML server controls must be within a <form> tag with the
runat="server" attribute. The runat="server" attribute indicates that the form
should be processed on the server. It also indicates that the enclosed
controls can be accessed by server scripts.

The System.Web.UI.HtmlControls.HtmlControl base class contains all


of the common properties. HTML server controls derive from this class.
 For example, consider the HTML input control:
The following table describes the
HTML server controls:
ASP.NET Web Server Controls
Web server controls are special ASP. NET tags understood by the
server.
Like HTML server controls, Web server controls are also created on
the server and they require a runat="server" attribute to work.
However, Web server controls do not necessarily map to any existing
HTML elements and they may represent more complex elements.
Mostly all Web Server controls inherit from a common base class,
namely the WebControlclass defined in
the System.Web.UI.WebControls namespace.
The syntax for creating a Web server control is:
The following table describes the WEB
server controls:
ASP.NET Validation Server Controls
 After you create a web form, you should make sure that mandatory fields of the
form elements such as login name and password are not left blank; data inserted
is correct and is within the specified range. Validation is the method of
scrutinizing (observing) that the user has entered the correct values in input
fields.
 A Validation server control is used to validate the data of an input control. If the
data does not pass validation, it will display an error message to the user.
 In ASP. NET you can use ASP. NET Validation Controls while creating the
form and specify what ASP. NET Validation Controls you want to use and to
which server control you want bind this.
 Validation Controls are derived from a common base class and share a common
set of properties and methods. You just have to drag and drop the ASP. NET
Validation Control in the web form and write one line of code to describe its
functionality.
 This reduces the developer time from writing JavaScript for each type of
validation. Moreover, through ASP. NET Validation Controls if any invalid data
is entered the browser itself detects the error on the client side and displays the
error without requesting the server. This is another advantage because it reduces
the server load.
Some Server Validation controls are:
Server Controls
HTML Server controls are similar to the HTML controls, except
they are processed by the server
Add runat = "server" to the HTML control to transform it into an
HTML Server control
HTML control:
<input type="text">
HTML Server control:
<input type="text" runat="server"/>
<input type=”radio” runat=”server” value=”Yes”/> Yes
Server-side programs can interact with the control before it is
rendered as a plain HTML control and sent to the browser
ASP.NET Controls
ASP.NET form controls will create the HTML code
ASP.NET Server controls are organized as:
 ASP.NET Form Controls
 Data Validation Controls
 User Controls
 Mobile Controls
ASP.NET controls are usually identified with the prefix asp:
followed by the name of the control
ASP.NET button:
<asp:Button id="ShowBtn" runat="server"
Text="Show the message." />
HTML Server Vs ASP.NET Server
Controls
ASP.NET form controls can interact with client-side events such as
when the user clicks on a button
 When the event occurs, ASP.NET can trigger a script to run on
the server
ASP.NET form controls also have different properties than their
HTML server control counterparts
 HTML Server label control
 Message1.InnerHTML = "Product 1"
 ASP server label control
 Message2.Text = "Product 2"
User Controls
User controls are external files that can be included within another
WebForm
User controls allow you to reuse code across multiple files
For example, you can create a user control that displays the a
navigation bar
You can use this control on the home page; they are often used for
creating self-contained code, headers, menus, and footers
User controls replace the functionality of ASP server-side include
pages
They are identified with the file extension .asmx
Other ASP.NET Server Controls
Data validation controls

 A series of controls that validate form data without extensive


JavaScript programming
Mobile controls

 A series of controls that provide form functionality within


wireless and mobile devices
Literal controls

 Page content that is not assigned to a specific HTML control


such as a combination of HTML tags and text to the browser
Server Controls within
Visual Studio .NET
In Visual
Studio .NET most of
the ASP.NET Server
controls are located
on the Web Forms
tab in the toolbox

Server controls with Visual Studio.NET


Code Behind file
The location of the code behind the page is determined via a property
that is set on the first line in the page using the @Page directive
<%@ Page Language="vb" Codebehind="WebForm1.vb"
Inherits=“MyFirstApp.WebForm1"%>
The @Page directive allows you to set the default properties for the
entire page such as the default language
The CodeBehind property identifies the path and filename of the code
behind file
The Inherits property indicates that the code behind the page inherits
the page class
This page class contains the compiled code for this page
Compiling the Page Class
The compiled code behind the page is the class definition for the
page
 A class is a named logical grouping of code
 The class definition contains the functions, methods, and
properties that belong to that class
In Visual Studio .NET the process of compiling a class is called
building
 When you build the application, you compile the code into an
executable file
 Visual Studio .NET compiles the code behind the page into an
executable file and places the file in the bin directory
Page Class Events
The Page Class consists of a variety of methods, functions,
and properties that can be accessed within the code behind the
page
The first time a page is requested by a client, a series of page
events occurs
The first page event is the Page_Init event which initializes
the page control hierarchy
The Page_Load event loads any server controls into memory
and occurs every time the page is executed
Page class events
Page_init
 Page_load
 Server_Controls
 Page_prerender
 Page_Unload
Web Services
Web Services also provide a means to expose .NET
functionality on the web but Web Services expose
functionality via XML and SOAP (cf: function calls
over the web)
Web Services
If your business partner is Course Technology and
you want to query that company’s product catalog
from your Web site, you could:
Post a link
Scrape a Web site (use a program to view a Web site and
capture the source code)
Provide a Web Service to their catalog application
Web Services are used to create business-to-business
applications
Web Services allow you to expose part or all of your programs
over the Internet. The Web Service source file has the extension
.asmx
A public registry known as UDDI contains registered public
Web Services. Third party Web Services are available at
http://www.xmethods.com
How ASP.NET works
When .NET is installed, IIS is configured to look
for files with the .aspx extension and to use the
ASP.NET module (aspnet_isapi.dll) to handle
them.
ASP.NET parses the .aspx file and arranges it in a
predefined class definition and generates an
asp.net page object.
The page object generates html that is sent back
to IIS and then the browser.
NOTE: only .aspx files are parsed (if it is pure html
don’t save it as an aspx file as it will slow down the
server.
ASP.NET samples
Page directives: <%@ page language = “VB”
debug="true" trace="true“ %>
<script language = “VB” runat=“server”> VB.NET
code declarations ……….. </script>
Message.aspx
<html>
<head> <title>Inserting ASP.NET code Example</title> </head>
<body>
Line1: First HTML Line<br />
Line2: Second HTML Line<br />
Line3: Third HTML Line<br />
</body>
</html>
 Note this has no asp code so better to use .html extension
Message2.aspx
<script language="VB" runat="server">
Sub Page_Load()
Response.Write ("First ASP.NET Line<br />")
Response.Write ("Second ASP.NET Line<br />")
Response.Write ("Third ASP.NET Line<br />")
End Sub
</script>
<html> <head> <title>Inserting ASP.NET code Example</TITLE>
</head>
<body>
Line1: First HTML Line<br />
Line2: Second HTML Line<br />
Line3: Third HTML Line<br />
</body>
</html>
Message3.aspx
html>
<head><title>Inserting ASP.NET code Example</title></head>
<body>
Line1: First HTML Line<br />
Line2: Second HTML Line<br />
Line3: Third HTML Line<br />
<script language="VB" runat="server">
Sub Page_Load()
Response.Write ("First ASP.NET Line<br />")
Response.Write ("Second ASP.NET Line<br />")
Response.Write ("Third ASP.NET Line<br />")
End Sub
</script>
</body>
</html>
Render or inline code block –
interweave1.aspx
<html>
<head>
<title>Interweaving ASP.NET code and HTML Example</title>
</head>
<body>
Line1: First HTML Line<br />
<% Response.Write ("First ASP.NET Line<br />") %>
Line2: Second HTML Line<br />
<% Response.Write ("Second ASP.NET Line<br />") %>
Line3: Third HTML Line<br />
<% Response.Write ("Third ASP.NET Line<br />") %>
</body>
</html>

NOT RECOMMENDED.
Interweave2.aspx A Server
control
script language="VB" runat="server">
Sub Page_Load()
Message.Text="The ASP.NET line"
End Sub
</script>
<html>
<head> <title>Inserting ASP.NET code Example</TITLE>
</head>
<body>
First HTML Line<br/>
<asp:label id=Message runat="server"/> <br />
Second HTML Line<br/>
</body>
Web application project files
AssemblyInfo.vb Info about the compiled project file stored in
/bin and named project.dll
Global.asax Event handler commands visible to all web
forms in a project
Global.asax.resx Define application resources such as text
strings, images. Can change without
recompiling project.
Global.asax.vb Asp.net code for application events eg
session.start
Project.sln Stores links to all project files
Project.suo VS.NET IDE configuration info for the proj.
Project.vbproj Configuration and build settings for project
files.
Web application project files
cont.
Project.vbproj.webinfo URL to project web server
Project.vsdisco Enables search for web services
Styles.css Project style sheet
Web.config Project and folder configuration information
Webform.aspx Web form .aspx file;Html
Webform.aspx.resx Resources in corresponding web form
Webform.aspx.vb Code written for the form (code behind)
Bin\project.dll Compiled project output file (assembly)

Bin\project.pdb Debugging information used by developer


Viewing the Assembly
Create a simple class, compile the class into an
assembly, then view the class using the IL
Disassembler
Open Notepad and type the code shown:
' hello.vb - displays hello world
' Created 06/01/2002
Imports System
Public Module Hello
Sub Main()
Dim s1 As String = "1 - Hello World"
Console.WriteLine(s1)
End Sub
End Module
' Run this at the command line
' vbc hello.vb
Using the ILDASM to
View the Assembly and
Classes

Using the ILDASM to view the assembly and classes


Examples
quickstart – webforms
 Intro4 shows VIEWSTATE
 Intro6 shows a click event

 Intro7 shows a usercontrol with a calander

 Intro8 shows a db connection

 Intro9 & 10 show asp.net templates

 Intro11shows validation controls

 Intro13 shows code behind pages

Server directives eg trace and debug


trace
The lab environment.
Each machine is set up to be an IIS server –
http://localhost:1900/…..
You create your web projects with Visual Studio.Net.
VS.NET will create a subdirectory in c:/inetpub/wwwroot
for your project. You must copy this subdirectory when
moving to another machine or home.
URL
http://localhost:1900/MyfirstApp/homepage.aspx
Alternative to VS.Net is webmatrix
Some samples on another machine
http://interdev.csse.monash.edu.au/cse2030/ Interdev is not
accessible outside the Monash network.
ASP.NET Vs PHP
Feature PHP ASP.NET
HTML Yes Yes
CSS Yes Yes
‘php Templates’ Yes UserControls
ServerControls No Yes
(buttons,grids etc)

Javascript Yes Yes + Validation controls


Database Conn Yes Yes
Cookies & Sessions Yes Yes
VIEWSTATE No Yes
POSTBACK No Yes

You might also like