ASP Net Basic 1
ASP Net Basic 1
NET
ASP.NET Architecture
HTTP Runtime
Runtime Compilation
Parse ASPX Engine Generate Codebehind class
Request Request
ASPX File
Instantiate
Compile
Response Response
Request Request
Page Initialized
Response
Validation Controls
Declarative Programming
User Controls
Reusability
Web Forms
Dramatically easier, more productive
Requires a lot less code for a given job Enables much cleaner code organization
ASP page combines declarative tags (HTML, ASP directives, server controls and static text) with code ASP.NET separates the code and tags for better manageability single file code <tags> <tags> code separate files
Form1.asp
Form1.aspx
Form1.vb
Server Controls
Server-programmable objects
Properties, method and events
Encapsulate behavior, rendering Can render differently to support multiple browsers or other Web clients
Mobile device support should be in beta 2
Class Hierarchy
System.Object System.Web.UI.Control
Validation in ASP
Write custom code every time Client-side code becomes interwoven with content Server-side code works but you have to manually propagate state between trips to the client Usually lots of tedious code snippets that take a lot of time to get right
You can attach more than one validation control to an input Only works with a subset of controls
Types of Validation
Required Field Compare Range
Input MUST be entered Input must match a pre-defined value or other control value Input must be within a lower- and upper-range Input must conform to a regular expression pattern Uses a custom function you create Summarizes the results of all the validation controls
Summary
When input fails validation, the associated validation controls Text property is displayed A summary validation control displays the ErrorMessage properties for all the Validation controls on the page
If the Text property is empty, it displays the ErrorMessage property
Bind to Collections
<asp:ListBox datasource='<%# myArray %>'>
Bind to Expressions
Contact: <%# ( foo.FName + " " + foo.LName ) %>
Bind to DataSets
Grid.DataSource = myDataSet.Tables(0)
Binding syntax <%# someField %> is NOT evaluated unless/until DataBind Binding expressions just work as long as the result is the expected data type
You may need to coerce to the appropriate type, such as myVal.ToString
Complex Binding
Most applications are going to talk to a big backend database like SQL Server This is more difficult than simple binding
Going to use ADO.NET Have to deal with Inserts, Updates and Deletes Have to manage multiple interfaces for each type of operation
There are event handlers associated with each button put your code there to update the database Using a TemplateColumn in a grid gives you complete control over the GUI
You can put any HTML control in a cell
ASP.NET Caching
Caching in ASP.NET
Lots of things you can cache
Cache sets of data Cache fragments of a page Cache an entire page
Cache based on time (keep it for x seconds) Cache based on content (keep a copy for each unique value in this dataset) Cache based on dependencies (keep a copy until a file or other cache item changes
When you create a cache item you set the desired lifetime and rules Cache persists until expiration or application is recycled Use Cache.Insert to create item
Absolute expiration
Expires in one hour Cache.Insert("MyData", Source, null, DateTime.Now.AddHours(1), TimeSpan.Zero)
Relative expiration
Expires 20 minutes after it was last used Cache.Insert("MyData", Source, null, DateTime.MaxValue, TimeSpan.FromMinutes(20))
Caching Support
Full page output caching
Vary by params, language, user-agent Enables portions of pages to be cached Developers can cache arbitrary objects Multiple expiration policies, file change invalidation
Caching Directive
Add a <%@ OutputCache %> directive to the page Set the Duration property for how long it should be served from cache Set the VaryByParam to whatever GET/POST parameters should cause a new item to be cached
Example: <%@ OutputCache Duration="15" VaryByParam="none" %>
Uses VaryByControl parameter in OutputCache When the value of that control changes, a new entry is added to the cache for the duration
Thank You