Professional Documents
Culture Documents
NET
and Web Forms
Arijit Sengupta
S511 Module 5
S511 Module Structure
S511 Technologies for E-business
Slide 2 of 72
Learning Objectives
What is ASP.NET; why it was developed
ASP.NET features
Programming Web Forms
Slide 3 of 72
Agenda
Background
ASP.NET Overview
Programming Model
Programming Basics
Server Controls
Data Binding
Conclusion
Slide 4 of 72
Background
Web Architecture
PC/Mac/Unix/...
Client
+ Browser
Request:
http://www.digimon.com/default.asp
Response:
<html>….</html>
Slide 5 of 72
Background
Web Development Technologies
Client-side technologies
HTML, DHTML, JavaScript
Server-side technologies
ASP (Active Server Pages)
ASP.NET is the next generation of ASP
Slide 6 of 72
Background
What is ASP?
Slide 7 of 72
Background
What is ASP?
ASP page
(static HTML,
server-side logic)
Slide 8 of 72
Background
Demo: HelloWorld.asp
<html>
<head><title>HelloWorld.asp</title></head>
<body>
<form method=“post">
<input type="submit" id=button1 name=button1
value="Push Me" />
<%
if (Request.Form("button1") <> "") then
Response.Write("<p>Hello, the time is " &
Now())
end if
%>
</form>
</body>
</html>
Slide 9 of 72
Background
ASP Successes
Slide 11 of 72
Agenda
Background
ASP.NET Overview
Programming Model
Programming Basics
Server Controls
Data Binding
Conclusion
Slide 12 of 72
ASP.NET Overview
ASP.NET provides services to allow the
creation, deployment, and execution of
Web Applications and Web Services
Like ASP, ASP.NET is a server-side technology
Web Applications are built using Web Forms
Web Forms are designed to make building
web-based applications as easy as building
Visual Basic applications
Slide 13 of 72
ASP.NET Overview
Goals
Keep the good parts of ASP and improve the rest
Simplify: less code, easier to create and maintain
Multiple, compiled languages
Fast
Scalable
Manageable
Available
Customizable and extensible
Secure
Tool support
Slide 14 of 72
ASP.NET Overview
Key Features
Slide 17 of 72
ASP.NET Overview
Architecture
VB C++ C# JScript …
Visual Studio.NET
Common Language Specification
ASP.NET: Web Services Windows
and Web Forms Forms
ADO.NET: Data and XML
Base Classes
Common Language Runtime
Slide 18 of 72
Agenda
Background
ASP.NET Overview
Programming Model
Programming Basics
Server Controls
Data Binding
Conclusion
Slide 19 of 72
Programming Model
Controls and Events
Slide 20 of 72
Programming Model
Controls and Events
Button code
Button ...
List code
List ...
Text code
Text ...
Slide 21 of 72
Programming Model
ASP.NET Object Model
Slide 22 of 72
Programming Model
Postbacks
Slide 23 of 72
Programming Model
Postbacks Maintain State
Slide 24 of 72
Programming Model
Server-side Controls
Slide 25 of 72
Programming Model
Automatic Browser Compatibility
Slide 26 of 72
Programming Model
Automatic Browser Compatibility
IE 4
Button
Menu
Text
Text
Menu Control Menu code
...
IE 5.5
Button
Text Control Text code
Menu
Text
...
IE 6
Button
...
Slide 27 of 72
Programming Model
Code-behind pages
Slide 29 of 72
Programming Model
Automatic Compilation
Slide 30 of 72
Agenda
Background
ASP.NET Overview
Programming Model
Programming Basics
Server Controls
Data Binding
Conclusion
Slide 31 of 72
Programming Basics
Page Syntax
The most basic page is just static text
Any HTML page can be renamed .aspx
Pages may contain:
Directives: <%@ Page Language=“VB” %>
Server controls: <asp:Button runat=“server”>
Code blocks: <script runat=“server”>…</script>
Data bind expressions: <%# %>
Server side comments: <%-- --%>
Render code: <%= %> and <% %>
Use is discouraged; use <script runat=server> with code in
event handlers instead
Slide 32 of 72
Programming Basics
The Page Directive
Lets you specify page-specific attributes, e.g.
AspCompat: Compatibility with ASP
Buffer: Controls page output buffering
CodePage: Code page for this .aspx page
ContentType: MIME type of the response
ErrorPage: URL if unhandled error occurs
Inherits: Base class of Page object
Language: Programming language
Trace: Enables tracing for this page
Transaction: COM+ transaction setting
Only one page directive per .aspx file
Slide 33 of 72
Programming Basics
Server Control Syntax
Slide 34 of 72
Programming Basics
Server Control Properties
Tag attributes map to control properties
<asp:button id=“c1" Text="Foo" runat=“server”>
<asp:ListBox id=“c2" Rows="5" runat=“server”>
c1.Text = “Foo”
c2.Rows = 5
Slide 35 of 72
Programming Basics
Maintaining State
By default. controls maintain their state across
multiple postback requests
Implemented using a hidden HTML field:
__VIEWSTATE
Works for controls with input data (e.g. TextBox,
CheckBox), non-input controls (e.g. Label,
DataGrid), and hybrids (e.g. DropDownList,
ListBox)
Can be disabled per control or entire page
Set EnableViewState=“false”
Lets you minimize size of __VIEWSTATE
Slide 36 of 72
Programming Basics
Maintaining State
Demo: MaintainingState.asp,
MaintainingState.aspx
Slide 37 of 72
Programming Basics
Server Code Blocks
Slide 38 of 72
Programming Basics
Page Events
Slide 39 of 72
Programming Basics
Page Event Lifecycle
Initialize Page_Init
Restore Control State
Load Page Page_Load
Control Events
1. Change Events Textbox1_Changed
Slide 40 of 72
Programming Basics
Page Loading
Slide 41 of 72
Programming Basics
Page Loading
if (Page.IsPostBack) then
else
' Executes only on initial page load
Message.Text = "initial value"
' Rest of procedure executes on every request
end sub
Slide 42 of 72
Programming Basics
Server Control Events
Change Events
By default, these execute only on next action event
E.g. OnTextChanged, OnCheckedChanged
Change events fire in random order
Action Events
Cause an immediate postback to server
E.g. OnClick
Works with any browser
No client script required, no applets,
no ActiveX® Controls!
Slide 43 of 72
Programming Basics
Wiring Up Control Events
Control event handlers are identified on the tag
<asp:button onclick="btn1_click“ runat=server>
<asp:textbox onchanged="text1_changed“ runat=server>
Slide 44 of 72
Programming Basics
Event Arguments
Slide 45 of 72
Programming Basics
Page Unloading
Slide 46 of 72
Programming Basics
Import Directive
Slide 47 of 72
Programming Basics
Page Class
The Page object is always available when
handling server-side events
Provides a large set of useful properties and
methods, including:
Application, Cache, Controls,
EnableViewState, EnableViewStateMac,
ErrorPage, IsPostBack, IsValid, Request,
Response, Server, Session, Trace, User,
Validators
DataBind(), LoadControl(), MapPath(),
Validate()
Slide 48 of 72
Agenda
Background
ASP.NET Overview
Programming Model
Programming Basics
Server Controls
Data Binding
Conclusion
Slide 49 of 72
Server Controls
ASP.NET ships with ~50 built-in controls
Organized into logical families
HTML controls
Controls / properties map 1:1 with HTML
Web controls
Richer functionality
More consistent object model
Slide 50 of 72
Server Controls
HTML Controls
Supported controls
<a> <textarea>
<img> <button>
<form> <input type=text>
<table> <input type=file>
<tr> <input type=submit>
<td> <input type=button>
<th> <input type=reset>
<select> <input type=hidden>
Slide 52 of 72
Server Controls
HTML Controls
Demo 1: HTMLControls1.aspx
Basic page lifecycle with HTML Controls
Demo 2: HTMLControls2.aspx
More HTML Controls
Slide 53 of 72
Server Controls
HTML Controls
Slide 54 of 72
Server Controls
Web Controls
Richer functionality
E.g. AutoPostBack, additional methods
Automatic uplevel/downlevel support
E.g. validation controls
Strongly-typed; no generic control
Enables better compiler type checking
Slide 55 of 72
Server Controls
Web Controls
Slide 57 of 72
Server Controls
Web Controls
Slide 58 of 72
Server Controls
Intrinisic Controls
Slide 59 of 72
Server Controls
Intrinisic Controls
Slide 60 of 72
Server Controls
List Controls
Slide 61 of 72
Server Controls
List Controls
Slide 62 of 72
Server Controls
CheckBoxList & RadioButtonList
Slide 63 of 72
Server Controls
Intrinisic & Simple List Controls
Demo 1: WebControls1.aspx
Assorted intrinsic and list controls
Demo 2: WebControls2.aspx
Same controls with AutoPostBack
Slide 64 of 72
Server Controls
Rich Controls
Slide 65 of 72
Server Controls
Validation Controls
Slide 66 of 72
Server Controls
Validation Controls
<asp:RequiredFieldValidator>
Ensures that a value is entered
<asp:RangeValidator>
Checks if value is within minimum and maximum values
<asp:CompareValidator>
Compares value against constant, another control or data type
<asp:RegularExpressionValidator>
Tests if value matches a predefined pattern
<asp:CustomValidator>
Lets you create custom client- or server-side validation function
<asp:ValidationSummary>
Displays list of validation errors in one place
Slide 67 of 72
Server Controls
Validation Controls
Slide 68 of 72
Server Controls
Validation Controls
Validation controls are associated with their
target control using the ControlToValidate
property
<asp:RequiredFieldValidator id="Req1"
ControlToValidate="TextBox1"
Text="Required Field" runat=server />
Slide 70 of 72
Server Controls
Validation Controls
Slide 71 of 72
Server Controls
Validation Controls
Slide 72 of 72
Server Controls
Validation Controls
Demo: ValidationControls1.aspx
Demonstrates each type of validation control
Slide 73 of 72
Agenda
Background
ASP.NET Overview
Programming Model
Programming Basics
Server Controls
Data Binding
Conclusion
Slide 74 of 72
Data Binding
How to Populate Server Controls?
Demo: DataBinding1.aspx
Data binding to simple, scalar expressions
Slide 79 of 72
Data Binding
Simple Lists
Data binding a list creates a user interface
element for each item in the list
Each item contains text (displayed to user) and
an optional value (not displayed)
The simple list controls:
<asp:ListBox>
Single or multiple select
<asp:DropDownList>
<asp:RadioButtonList>
<asp:CheckBoxList>
Slide 80 of 72
Data Binding
Simple Lists
Slide 81 of 72
Data Binding
Simple Lists
Demo: DataBinding2.aspx
Data binding to simple lists
Slide 82 of 72
Resources
General Sites
http://msdn.microsoft.com/net/aspnet/default.asp
http://www.asp.net/
http://www.fmexpense.com/quickstart/aspplus/defa
ult.htm
http://www.asptoday.com/
http://www.aspng.com/aspng/index.aspx
http://www.4guysfromrolla.com/
http://www.aspfree.com/
http://www.devx.com/dotnet/
http://www.ibuyspy.com/
Slide 83 of 72
Resources
ASP.NET Overview
http://msdn.microsoft.com/msdnmag/issues/0900/ASPPlus/AS
PPlus.asp
Validation
http://msdn.microsoft.com/library/techart/aspplusvalid.h
tm
Databinding in 3 parts
http://msdn.microsoft.com/msdnmag/issues/01/03/cutting/c
utting0103.asp
http://msdn.microsoft.com/msdnmag/issues/01/04/cutting/c
utting0104.asp
http://msdn.microsoft.com/msdnmag/issues/01/05/cutting/c
utting0105.asp
ASP.NET component model
http://msdn.microsoft.com/msdnmag/issues/01/02/cutting/c
utting0102.asp
Slide 84 of 72