Designed to emulate the control-based interface of a Windows application
Two problems
Web applications execute on the server
Events occur on the client
Events are processed on the server
Solutions
Postback
Automatic Postback
Posting back without hitting the submit button
Example
Changing a selection in a list
Clicking a checkbox
How it works
set AutoPostBack property to true
a JavaScript function is added called _doPostBack() which triggers the posting of the form
the control's onclick, onchange, etc property is set to this function
two hidden input fields which convey
ID of the control that raised the event
any additional information
doPostBack() sets the value of these fields before posting the form
Any additional controls using AutoPostBack are tied in to the doPostBack() function
doPostBack() is the intermediary between the client and the server
Web applications are stateless
Before the rendered HTML is sent back all objects and client-specific information is trashed
Solutions
View State
When page is posted back
ASP.NET re-creates the page and control objects to their default state (using aspx)
viewstate information is deserialized and used to update all the controls
The page is now in the state it was in right before being sent to the client the last time
The page is adjusted according to the posted back form data
The page is now in the state it was in as the user last saw it, right before being posted back
the appropriate events are triggered and the code reacts
Upside is Scalability due to server resources being freed after each request
Downsides
Larger page size
takes longer to receive
takes longer to post
Insecure
Client can read the data
Encryption can prevent reading
Client can modify the data
Hash codes can prevent tampering
Viewstate can be disabled per control with EnableViewState=false
How it works
When form data is received, your code may make changes to the page
These changes are not reflected in the form that is posted back
so after the returned form is processed but before it is sent back all the properties on all the controls on the page are examined
If any property is different that default, the data is entered in a hidden field in the <form>
Proxy servers and firewalls may have problems with hidden fields containing too much data
View State Chunking breaks up the view state data among several hidden fields
Running an HTML form
1. user clicks the submit button
2. browser collects the value of each control and pastes it together into a single string
Example
FirstName=Matthew&LastName=MacDonald&CS=on&VB=on
Rules
all data sent occurs in name/value pairs
all name/value pairs are separated with an ampersand
each name/value pair is split with an equal sign
check boxes are left out unless they are checked
3. the string is sent to the page indicated in the form tag using an HTTP POST operation
Running a Web Form
1. read the entire .aspx file
2. generate the corresponding objects
3. fire a series of events
These events are responded to through code
Control model
a layer of abstraction is placed over the raw form data
original form data is available in Request.Form collection
Request.Form["FirstName"];
This way the values returned are type safe: a checkbox returns a boolean value
Advantages
type safe
readable and writable instead of just readable
Event model
Process
1. Page is requested
2. Page and control objects are created
3. Initialization code executes
4. Page is rendered to HTML and returned to the client
5. Page and control objects are released from memory
6. A postback is triggered
7. All form data is submitted back to the server
8. ASP.NET reads the form data and re-creates the page objects, returning them to the state they were in the last time the page was sent
9. Appropriate events are triggered (depending on the control that triggered the postback)
10. The modified page is rendered to HTML and returned to the client.
11. Page and control objects are released from memory
Web Form Processing Stages
1. Page framework initialization
Page is first created
controls defined with tags in the aspx are generated
view state is de-serialized and applied to the controls
Page.Init event fires
Rarely handled because view state and controls may not exist yet
2. User code initialization
Page.Load event fires
Used to perform required initialization such as filling in dynamic text or configuring controls
IsPostBack has two uses
Since view state is maintained automatically, dynamic data need only be fetched on first page load leading to a performance boost
Change the interface after page's first use
Avoid initializing control properties in Page.Load or set EnableViewState=false because all changes occurring in events are persisted in view state
3. Validation
Validation controls fire
These are rarely responded to
These can be checked using Page.IsValid in another event handler
4. Event Handling
events that have taken place since the last postback are fired
Two types
Immediate response events
Any control that triggered the _doPostBack() function
If AutoPostBack is set to true
If the control was designed to post back (submit button)
Change events
Anything that changed but did not trigger a post back
5. Automatic data binding
Cleanup