740 likes | 907 Views
Developing Windows and Web Applications using Visual Studio.NET. Drew Robson. Session 6: ASP.NET. The course ten sessions – Overview so far Overview of ASP.NET An ASP.NET Page Server Controls User Controls Validation Master Pages Themes & skins.
E N D
Developing Windows and Web Applications using Visual Studio.NET Drew Robson
Session 6: ASP.NET The course ten sessions – Overview so far Overview of ASP.NET An ASP.NET Page Server Controls User Controls Validation Master Pages Themes & skins
The 10 SessionsFirst 5 – Done – Winforms • Overview of .NET, OOP and Visual Studio • Data in Forms • Usability - Rules to Better Windows Forms • Deployment of Windows Forms and Design Patterns • Web Services (WCF) and Threading
The 10 SessionsNext 5 – To Do – Webforms • Overview of .NET Webforms TODAY • Data in Webforms • Usability and Reporting • Silverlight • MVC, jQuery and AJAX
HTML • HyperTextMarkupLanguage • Describes a web page http://en.wikipedia.org/wiki/HTML <html> <head> <title>Hello HTML</title> </head> <body> <p>Hello World!</p> </body> </html>
HTTP • Hypertext Transfer Protocol • Request – Response http://en.wikipedia.org/wiki/Http GET /index.html HTTP/1.1 Host: www.example.com HTTP/1.1 200 OK Date: Mon, 23 May 2005 22:38:34 GMT Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT Content-Length: 438 Content-Type: text/html; charset=UTF-8
How a web page is shown Your Computer Hosting Computer Internet The Internet Server Client
Request / Response Request www.ssw.com.au The Internet Response Server HTML Client
.NET Overview • IL = Intermediate Language • CLR = Runtime
What Is ASP.NET? ASP.NET provides a complete environment for building, deploying, and running .NET Web applications. • Developer Productivity • Simplified page development model • Target any Web client (PC or mobile device) • Modular, well-factored, extensible architecture • Superior debugging and tracing support • Enhanced Performance, Scalability, and Reliability • Compiled, not interpreted • Rich caching support • Web farm scalable session state • Automatically detects and recovers from errors • Simple Deployment and Configuration • No need to bring down Web server • Deploy and upgrade running applications with XCOPY • XML configuration files
Request / Response Request ASP.NET www.ssw.com.au The Internet Response Server HTML Client
ASP.NET Page Request • User requests an application resource from the Web server. • IIS forwards the call to ASP.NET’s process • Application manager calls the Application domain & Processes the page.
Parse Generate Code-behindclassfile ASPX Engine Request Gen’dPageClassFile ASPX File Instantiate Request Page Class Response (HTML/js/dhtml/etc…) Instantiate, process and render ASP.NET Compilation
2 Types of Projects – 1 – Web Site Web Site • Don’t use this, here for compatibility only • File > New > Web Site • Each page is dynamically loaded into memory • Slow on first load after deployment • No need to recompile for code change
2 Types of Projects – 2 – Web Application Web Application - • Recommended • File > New > Projects, then Select Web Application • Compiles all pages into one DLL • Faster on first load after deployment • Must recompile whole site for code change • Not available in Default VS 2005, requires SP2
Parse Generate Code-behindclassfile ASPX Engine Request Gen’dPageClassFile ASPX File Instantiate Request Page Class Response (HTML/js/dhtml/etc…) All pre compiled! ASP.NETCompilation Web Application
ASP.NET –Page/Web Form An ASP.NET Web page consists of two parts: • Visual elements, which include markup, server controls, and static text. • Programming logic for the page, which includes event handlers and other code.
ASP.NET – Page/Web Form Things to Notice
Things to Notice • Page Directive ASP.NET – Page/Web Form
Things to Notice • Page Directive • Server side code ASP.NET – Page/Web Form
Things to Notice • Page Directive • Server side code • Form ASP.NET – Page/Web Form
Things to Notice • Page Directive • Server side code • Form • Normal HTML Structure ASP.NET – Page/Web Form
Things to Notice • Page Directive • Server side code • Form • Normal HTML Structure • Server Controls ASP.NET – Page/Web Form
ASP.NET – Page Code Model ASP.NET provides two models for managing the visual elements and code: • The Single-File Page Model <%@ Page Language="VB“> <script runat=“server”> … </script> • The Code-Behind Page Model <%@ Page Language="VB“ CodeFile="SamplePage.aspx.vb“ Inherits="SamplePage“ AutoEventWire="false" %>
ASP.NET – Page Life Cycle Stages (SILVER-U) • Page request • Start • Page Initialization • Load • Validation • PostbackEvent handling • Rendering • Unload http://msdn.microsoft.com/en-us/library/ms178472.aspx
ASP.NET – Page Life Cycle Stages (SILVER-U) 1) Page request • Occurs before the page life cycle begins. • When the page is requested by a user • ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or if a cached version of the page can be sent in response without running the page.
ASP.NET – Page Life Cycle Stages (SILVER-U) 2) Start • Page properties set • Request and • Response • Determines whether the request is a postback or a new request and sets the IsPostBack property. • Sets the page's UICulture property
ASP.NET – Page Life Cycle Stages (SILVER-U) 3) Page Initialization • Controls on the page are available • Each control's UniqueID property is set. • Themes are applied to the page. • If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.
ASP.NET – Page Life Cycle Stages (SILVER-U) 4) Page Load • If the current request is a postback, control properties are loaded with information recovered from view state and control state.
ASP.NET – Page Life Cycle Stages (SILVER-U) 5) Page Validation • The Validate method of all validator controls is called • Sets the IsValid property of • Individual validator controls • The page
ASP.NET – Page Life Cycle Stages (SILVER-U) 6) Postback Event Handling • If the request is a postback, • Event handlers are called
ASP.NET – Page Life Cycle Stages (SILVER-U) 7) PageRendering • Before rendering, view state is saved for the page and all controls. • During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page's Response property.
ASP.NET – Page Life Cycle Stages (SILVER-U) 8) Page Unload • Unload is called after the page has been: • Fully rendered, • Sent to the client, and • Is ready to be discarded. • At this point, page properties such as Response and Request are unloaded and any cleanup is performed.
ASP.NET – Page Life Cycle Stages (SILVER-U) • Page request • Start • Page Initialization • Load • Validation • PostbackEvent handling • Rendering • Unload http://msdn.microsoft.com/en-us/library/ms178472.aspx
ASP.NET – Page Life Cycle Events • PreInit • Init • InitComplete • PreLoad • Load • Control events • LoadComplete • PreRender • SaveStateComplete • Render • Unload
Client submits data back to the server E.g. Submission of a form Page Postback
Page Viewstate Allows the state of objects (serializable) to be stored in a hidden field on the page ViewState is transported to the client and back to the server Is not stored on the server or any other external source. ViewState is used to retain the state of server-side objects between postbacks Server can see if values have changed
ASP.NET – Server Controls Server controls are tags that are understood by the server. Syntax: <asp:control_name id="some_id" runat="server" /> Example: <asp:Button id="button1" Text="Click me!" runat="server" OnClick="submit" />
Demo How to create a web application How to create a web form Designer Features Different Page Code models Postbacks Viewstate, Session State, Application State
ASP.NET User Controls A group of server controls that are created by the user. Encapsulates certain functionality Can be used on multiple pages E.g Address User control (in your lab) Contact User Control
ASP.NET Configuration Web.Config Similar to app.config in windows Application-wide configuration Provide application settings In XML, so it’s easy to change