Sunteți pe pagina 1din 77

Developing Windows and Web Applications using Visual Studio.

NET

Peter Gfader
Senior Software Architect

Peter Gfader

SSA @ SSW Loves C# and .NET (Java not anymore) Specializes in


Windows Forms ASP.NET TFS testing Automated tests Silverlight

Homework?

Consume a public web service Show users where they are

Session 6: ASP. NET


The course ten sessions Overview so far Overview of ASP.NET Page

An ASP.NET

Server Controls User Controls Validation

Master Pages
Themes & skins

LAB

Page Cycle Events

Menu, Navigation & Sitemaps


Some cool new ASP.NET 2 Server Controls

The 10 Sessions First 5 Done Winforms

http://sharepoint.ssw.com.au/Training/UTSNET/

Part 1: .NET Winforms


1. 2. 3. 4. 5.

Overview of .NET Data in Forms Usability - Rules to Better Windows Forms Deployment and Security of Windows Forms Web Services and Threading

The 10 Sessions Next 5 To Do Webforms

http://sharepoint.ssw.com.au/Training/UTSNET/

Part 2: .NET Webforms


1. 2. 3.

4. 5.

Overview of .NET Webforms TODAY Data in Webforms Usability Rich Web Forms and Other ASP.NET Features Web Security Advanced Topics & Future Technology (Silverlight)

The web

HTML

HyperText Markup Language Describes a web page <html> <head> <title>Hello HTML</title> </head> <body> <p>Hello World!</p> </body> </html>

http://en.wikipedia.org/wiki/HTML

HTTP

Hypertext Transfer Protocol Request Response


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
http://en.wikipedia.org/wiki/Http

How a web page is shown


Your Computer The Internet Internet
Client Server Hosting Computer

Request / Response
Request www.ssw.com.au The Internet Response Client HTML Server
Files

.NET Overview

IL = Intermediate Language CLR = Runtime

CLR

Common Language Runtime

= Virtual machine

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 Compiled, not interpreted Rich caching support Web farm scalable session state Automatically detects and recovers from errors No need to bring down Web server Deploy and upgrade running applications with XCOPY XML configuration files

Enhanced Performance, Scalability, and Reliability


Simple Deployment and Configuration


ASP.NET Page Request


1. 2. 3.

User requests an application resource from the Web server. IIS forwards the call to ASP.NETs process Application manager calls the Application domain & Processes the page.

.NET Framework

Evolution The whole .NET FX


http://shrinkster.com/1515

ASP.NET Compilation

Parse

ASPX Engine

Generate

Codebehind class file

Request Request

ASPX File

Instantiate

Gend Page Class File

Response (HTML/js/dhtml/etc)

Page Class

Instantiate, process and render

2 Types of Projects 1 Web Site


Web Site

Dont 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

ASP.NET
Compilation

Web Site Parse ASPX Engine Generate Codebehind class file

Request Request

ASPX File

Instantiate

Gend Page Class File

Response (HTML/js/dhtml/etc)

Page Class

Instantiate, process and render

ASP.NET
Compilation

Parse

ASPX Engine

Generate

Codebehind class file

Web Application Request Request ASPX File Instantiate Gend Page Class File

Response (HTML/js/dhtml/etc) All pre compiled!

Page Class

The Page

ASP.NET Page/Web Form


An ASP.NET Web page consists of two parts:
1.

Visual elements, which include markup, server controls, and static text. Programming logic for the page, which includes event handlers and other code.

2.

ASP.NET Page/Web Form


Things to Notice

ASP.NET Page/Web Form


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 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" %>

A Page's Life

ASP.NET Page Life Cycle Stages (SILVER-U)


1. 2. 3. 4. 5. 6. 7. 8.

Page request Start Page Initialization Load Validation Postback Event 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) Page Rendering


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)


1. 2. 3. 4. 5. 6. 7. 8.

Page request Start Page Initialization Load Validation Postback Event handling Rendering Unload

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Postback & ViewState

ASP.NET Postbacks
A Page Postback is:

Where the client communicates back to the server, Through the page that was originally served. The post back is a submission of the Form element.

ASP.NET Viewstate
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.

So program can see if values have changed

Server states

ASP.NET States

Session State allows the state of objects (serializable) to be stored for a single session (lifetime of the users browser or specific timeout)

Application State allows the state of objects (serializable) to be stored for the application across different sessions.

Server controls

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

User controls

ASP.NET User Controls


A group of server controls that are created by the user. Encapsulates certain functionality

Can be used on multiple pages


Address User control (in your lab) Contact User Control

E.g

ASP.NET Configuration

ASP.NET Configuration
Web.Config

Similar to app.config in windows Application-wide configuration Provide application settings In XML, so its easy to change

Who is the Master?

ASP.NET 2: Master Pages


Master pages new concept in ASP.NET 2.0 Allows site developers to build master templates for their site's look and feel Put common code shared by all the pages on the master page A page that references a Master Page is called a Content Page.

ASP.NET 2: Master Pages

How to define the Master page

At the page level (in the page) <%@ Page Language="C# MasterPageFile="MySite.Master" %>

At the application level (in web.config) <pages masterPageFile="MySite.Master" />

Validation

ASP.NET: Validation
A Validation server control is used to validate the data of an input control. If the data does not pass validation, it will display an error message to the user.

ASP.NET: Validation
Important Properties:

ControlToValidate: The ID of the control that this validator validates.

Display: Has three possible values:


Dynamic space the control uses isnt reserved for the control Static space control uses is always reserved None control is invisible

EnableClientScript: Validation occurs on the Clients Browser (default).


Text: Displayed when validation fails; often used to put an asterisk or an icon next to the error or for displaying the error message in a validation summary.

Themes & Skins

ASP.NET 2: Themes & Skins


What is a theme? A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in a Web application, across an entire Web application, or across all Web applications on a server. A theme contains:

skins, cascading style sheets (CSS), images, and other resources

ASP.NET 2: Themes & Skins


What is a skin file? A skin file has the file name extension .skin Belongs to a certain theme Contains property settings for individual controls Contains settings for server controls only

ASP.NET 2: Themes & Skins


For Example: <asp:button ID=btnNew

With css (before skin files were around):


<asp:button ID=btnNew runat=server cssclass=blueBlackButton/> .blueBlackButton { Background:lightblue; Color:Black; }

ASP.NET 2: Themes & Skins


But with Skins, you let the skin do the design work. In the page: <asp:button ID=btnNew runat="server" /> In the Skin file: <asp:button runat="server"

BackColor="lightblue"
ForeColor="black" />

ASP.NET 2: Themes & Skins


So, when do we use css files? When you need to style non-server controls, because skin files are only for asp.net controls.

Theme File Structure


MyWebSite > App_Themes > BlueTheme > Controls.skin > BlueTheme.css > PinkTheme

> Controls.skin
> PinkTheme.css

Applying Themes
Apply a theme to a Web site <configuration> <system.web> <pages theme="ThemeName" /> </system.web> </configuration>

Apply a theme to an individual page <%@ Page Theme="ThemeName" %>

Done

So who thinks Win forms are better than web forms?

Resources

Firebug

http://getfirebug.com/

ASP.NET Webforms

http://www.asp.net/web-forms/

ASP.NET MVC

http://www.asp.net/mvc/

Resources

ASP.NET AJAX

http://www.asp.net/ajax/

Ramp up your skills

http://msdn.microsoft.com/rampup

Resources ASP.NET

Quickstarts

http://quickstarts.asp.net/

Master pages

http://www.asp.net/learn/master-pages/

Profiles, Themes and user controls

http://www.asp.net/learn/moving-to-asp.net-2.0/module-10.aspx

Page lifecycle

http://www.asp.net/learn/videos/video-6558.aspx

Resources ASP.NET

Validation

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/validation/defa ult.aspx

Configuration & Deploying

http://www.asp.net/learn/videos/video-05.aspx

User controls

http://www.asp.net/learn/videos/video-194.aspx

Resources - Usergroups

Silverlight Designer and Developer Network

http://www.sddn.org.au/

Sydney Deep .NET User Group

http://www.sdnug.org/

Newcastle Coders Group

http://www.ncg.asn.au/

Sydney .NET Users Group

http://www.ssw.com.au/ssw/NETUG/Sydney.aspx

3 things

PeterGfader@ssw.com.au http://peitor.blogspot.com twitter.com/peitor

Thank You!
Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA ABN: 21 069 371 900

Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105


info@ssw.com.au www.ssw.com.au

S-ar putea să vă placă și