Web Form Fundamentals
cont’d
Stages in IIS
ASP.NET Request
System.Web.HttpApplication
What really happens
when ASP.NET receives
a request for .aspx page?
System.Web.UI.Page
? ASP.NET
Platform
Services
the scene
Rendered
Behind
HTML
2
Illusion at Work
• In ASP.NET application the code works with objects
and runs in a protected environment on the server.
• The client only sees the results once the web page
processing has ended and the program has been
released from memory.
• It creates an illusion of a continuously running
application just similar to Windows applications.
3
Code-Behind Programming
Second type of code model in ASP.NET
• Create .aspx file to contain user interface logic
• Create .vb or .cs code file to match .aspx file
4
Part A “User Interface File”
<!-Code Behind Example>
<!-Use of Page Directive>
<%@ Page Language="VB" Inherits=“First" Src=“First.vb" %>
<%@ Page Language=“c#" Inherits=“First" Src=“First.cs" %>
<html>
<body>
<form id="Form" runat="server">
<asp:Label id="lblTest" runat="server" />
</form>
</body>
</html>
5
Part B “Code-Behind File”
Public Class First
Inherits System.Web.UI.Page
Protected lblTest As System.Web.UI.WebControls.Label
Public Sub Page_Load( )
lblTest.Text="Hello, the Page_Load event occurred."
End Sub
End Class
6
Web Form Inheritance
System.Web.UI
namespace
Custom Custom
Generic
Page Class .aspx file
Page Class
Inherited by (.vb/.cs)
Inherited by
creates
Page
Object
7
Building A Currency Converter Application
A simple page that allows the user to convert a number of US
dollars to the equivalent amount of Euros.
Other examples
– Calculators for mortgages, Taxes, Health indices, Saving
plans, Single-phrase translators, Stock tracking utilities
8
Currency Converter Interfaces
9
Improving Currency Converter
10
Problems with ASP
Development with ASP?
1. Heavy use of
Response.Write
2. Spaghetti code (statements
order)
3. Lack of flexibility
(modification)
4. Mixing of content and
formatting
5. Complexity
6. No state management of
User Interface (automatic)
11
Problems with ASP
ASP.NET High Level Model Programmers had to master
HTML for designing dynamic
web pages.
Extensive code changes were
required whenever UI was
Server Side Web Controls modified.
– Created and configured as objects
– Automatically provide HTML output
– State maintenance
– Raising events
– Analogy with windows programming
12
Server Controls
ASP.NET turn HTML tags into objects/controls that can be
programmed on the server.
1. HTML Server Controls
– Server-based equivalents of standard HTML elements
– Provide object interface
– Useful for easy migration (ASP->ASP.NET)
2. Web Controls
– Richer object model with more properties, events
– More closer to windows development
– Include advanced controls (e.g. Datagrid)
13
HTML Server Controls
Key features
1. Generate their interface
2. Retain state
3. Fire events
Using HTML Server Controls
attribute runat=“server”
attribute id=“uniquevalue”
14
HTML Server Control Classes
• System.Web.UI.HtmlControls namespace
• Separate class for each control
• Properties, methods, events
• Classes required for currency converter application
1. HtmlForm
2. HtmlInputButton
3. HtmlInputText
4. HtmlGenericControl
15
Structure of CCA User Interface
HtmlForm
instance
empty
HtmlInputText
HtmlInputButton HtmlGenericControl
instance instance instance
HtmlGenericControl
16
instance
Design View
17
XHTML Source View (.aspx)
18
Code View (.vb)
19
Handler for Page Load Event
20
Event Handler for Convert.ServerClick
21
Event Handler for ShowGraph.ServerClick
22