web forms. agenda web forms web controls code separation dynamic compilation system.web.ui.page user...

32
Web Forms Web Forms

Upload: tracy-benson

Post on 24-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Web FormsWeb Forms

Page 2: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

AgendaAgenda

Web formsWeb forms

Web controlsWeb controls

Code separationCode separation

Dynamic compilationDynamic compilation

System.Web.UI.PageSystem.Web.UI.Page

User controlsUser controls

Page 3: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

HTML Form (Calc.html)HTML Form (Calc.html)

<html> <body> <form> <input type="text" name="op1" /> + <input type="text" name="op2" /> <input type="submit" value=" = " /> </form> </body></html>

Page 4: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

ASP Form (Calc.asp)ASP Form (Calc.asp)

<%@ Language="VBScript" %>

<html> <body> <form> <input type="text" name="op1" value="<%= Request ("op1") %>"/> + <input type="text" name="op2" value="<%= Request ("op2") %>" /> <input type="submit" value=" = " /> <% If Request ("op1") <> "" And Request ("op2") <> "" Then a = CInt (Request ("op1")) b = CInt (Request ("op2")) Response.Write (CStr (a + b)) End If %> </form> </body></html>

Page 5: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Web Form (Calc.aspx)Web Form (Calc.aspx)

<html> <body> <form runat="server"> <asp:TextBox ID="op1" RunAt="server" /> + <asp:TextBox ID="op2" RunAt="server" /> <asp:Button Text=" = " OnClick="OnAdd" RunAt="server" /> <asp:Label ID="Sum" RunAt="server" /> </form> </body></html>

<script language="C#" runat="server">void OnAdd (Object sender, EventArgs e){ int a = Convert.ToInt32 (op1.Text); int b = Convert.ToInt32 (op2.Text); Sum.Text = (a + b).ToString ();}</script>

Page 6: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

ASP.NET Web FormsASP.NET Web Forms

Page 7: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Web ControlsWeb Controls

Approximately 80 in allApproximately 80 in all~30 carried forward from ASP.NET 1.x~30 carried forward from ASP.NET 1.x

~50 new controls in ASP.NET 2.0~50 new controls in ASP.NET 2.0

Rich programming model that Rich programming model that permits difficult UIs (such as menus permits difficult UIs (such as menus and tree views) to be encapsulated in and tree views) to be encapsulated in easy-to-use classeseasy-to-use classes

Open architecture that enables Open architecture that enables developers to write custom controls developers to write custom controls of their ownof their own

Page 8: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

““Simple” ControlsSimple” Controls

Name Description

Label Renders programmable HTML text

TextBox Renders programmable HTML text boxes

HyperLink Renders programmable HTML hyperlinks

CheckBox Renders programmable HTML check boxes

RadioButton Renders programmable HTML radio buttons

Image Renders programmable static images

Page 9: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

““Simple” Controls, Cont.Simple” Controls, Cont.

Name Description

Table Renders programmable HTML tables

Panel Provides container for grouping other controls

PlaceHolder Serves as placeholder for adding controls at run-time

HiddenField Renders hidden fields (<input type="hidden">)

ImageMap Renders programmable HTML image maps

FileUpload Provides UI for uploading files

Page 10: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Button ControlsButton Controls

Name Description

Button Renders push button that posts back and fires Click eventwhen clicked

LinkButton Renders hyperlink that posts back and fires Click eventwhen clicked

ImageButton Renders image that posts back and fires Click eventwhen clicked

Page 11: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Using the TextBox, Using the TextBox, Label, and Button Label, and Button ControlsControls

Page 12: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

List ControlsList Controls

Name Description

ListBox Renders progammable HTML list boxes

DropDownList Renders programmable HTML drop-down lists

RadioButtonList Renders programmable groups of HTML radio buttons

CheckBoxList Renders programmable groups of HTML check boxes

BulletedList Renders programmable bulleted lists

Page 13: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Using DropDownListUsing DropDownList

Page 14: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Validation ControlsValidation Controls

Name Description

CompareValidator Compares an input to another value

CustomValidator

RangeValidator

RegularExpressionValidator

RequiredFieldValidator

ValidationSummary

Validates input using an algorithm you supply

Verifies that an input falls within a specified range

Validates input using a regular expression

Verifies that an input field isn't blank

Displays a summary of validation errors

Page 15: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Input ValidationInput Validation

Page 16: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Code SeparationCode Separation

Web forms support two* coding Web forms support two* coding models:models:

Code-inline - Markup and code in ASPXCode-inline - Markup and code in ASPX

Code-behind - Markup in ASPX, code in Code-behind - Markup in ASPX, code in CS/VBCS/VB

Visual Studio 2005 supports both Visual Studio 2005 supports both modelsmodels

No difference in performanceNo difference in performance

* Not including code-behind 1.0, which is deprecated in ASP.NET 2.0

Page 17: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Code-InlineCode-Inline

<html> <body> <form runat="server"> <asp:TextBox ID="op1" RunAt="server" /> + <asp:TextBox ID="op2" RunAt="server" /> <asp:Button Text=" = " OnClick="OnAdd" RunAt="server" /> <asp:Label ID="Sum" RunAt="server" /> </form> </body></html>

<script language="C#" runat="server">void OnAdd (Object sender, EventArgs e){ int a = Convert.ToInt32 (op1.Text); int b = Convert.ToInt32 (op2.Text); Sum.Text = (a + b).ToString ();}</script>

Calc.aspx

Page 18: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Code-BehindCode-Behind

<%@ Page CompileWith="Calc.aspx.cs" ClassName="Calc_aspx" %>

<html> <body> <form runat="server"> <asp:TextBox ID="op1" RunAt="server" /> + <asp:TextBox ID="op2" RunAt="server" /> <asp:Button Text=" = " OnClick="OnAdd" RunAt="server" /> <asp:Label ID="Sum" RunAt="server" /> </form> </body></html>

Calc.aspx

Corresponding source code file

Class name in source code file

Page 19: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Code-Behind, Cont.Code-Behind, Cont.

Calc.aspx.cs

using System;using System.Web; ...

public partial class Calc_aspx{ void OnAdd (Object sender, EventArgs e) { int a = Convert.ToInt32 (op1.Text); int b = Convert.ToInt32 (op2.Text); Sum.Text = (a + b).ToString (); }}

Partial class representing this page(derives from System.Web.UI.Page)

Page 20: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Dynamic Page Dynamic Page CompilationCompilation

ParserParser

DLLDLL

HTTP Request HTTP Response

CompilerCompiler

Once compiled, assembly reusedin subsequentrequests

public partial class Calc_aspx : System.Web.UI.Page, ...{ ...}

public partial class Calc_aspx : System.Web.UI.Page, ...{ ...}

ASPXASPX

public partial class Calc_aspx{ // Your code}

public partial class Calc_aspx{ // Your code}

Parser generatespartial classderived from Page

Calc.aspx.cs

a0b1c3d4.cs

Source code filespassed to compiler

Compiler producesassembly containingPage-derived class

Page 21: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

PrecompilationPrecompilation

Applications can be precompiled to Applications can be precompiled to avoid first-access delays (new in avoid first-access delays (new in ASP.NET 2.0)ASP.NET 2.0)

Page 22: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

System.Web.UI.PageSystem.Web.UI.Page

Base class for pages; container for Base class for pages; container for controlscontrols

Your code executes in context of a Your code executes in context of a Page-derived class, providing intrinsic Page-derived class, providing intrinsic access to Page members (e.g., access to Page members (e.g., Request & Response)Request & Response)

Parser adds useful properties of its own Parser adds useful properties of its own to Page-derived classes (e.g., Profile & to Page-derived classes (e.g., Profile & Resources)Resources)

Page objects fire events that you can Page objects fire events that you can handle by implementing specially handle by implementing specially named methodsnamed methods

Page lifetime is one requestPage lifetime is one request

Page 23: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Key Page PropertiesKey Page Properties

Name Description

Cache Provides access to the application cache

Controls Provides access to the page’s controls

IsPostBack True if page is executing due to a postback

Request Encapsulates the current HTTP request

Response Encapsulates the outgoing HTTP response

Session Provides access to session state

Type

Cache

ControlCollection

Boolean

HttpRequest

HttpResponse

HttpSessionState

User Provides information about requestor’s identityIPrincipal

Master Provides programmatic access to master pageMasterPage

Page 24: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Using Page PropertiesUsing Page Properties

if (!String.IsNullOrEmpty (Request["ItemID"])) { Response.Write (Request["ItemID"]);}

Page.Request property refers to HttpRequestobject representing the request

Page.Response property refers toHttpResponse object representingthe response

Page 25: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

The Page_Load MethodThe Page_Load Method

If present, called automatically in If present, called automatically in every request when Page object fires every request when Page object fires Load eventLoad event

Called early in page's lifetimeCalled early in page's lifetimeControls have been created and Controls have been created and initializedinitialized

No rendering has been done (yet)No rendering has been done (yet)

Perfect place to initialize controls Perfect place to initialize controls programmatically and to bind programmatically and to bind controls to data sourcescontrols to data sources

Page 26: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Page_Load ExamplePage_Load Example

<html> <body> <form runat="server"> <asp:DropDownList ID="MyDropDownList" RunAt="server" /> </form> </body></html> ...void Page_Load (Object sender, EventArgs e){ if (!IsPostBack) { MyDropDownList.Items.Add ("Item 1"); MyDropDownList.Items.Add ("Item 2"); MyDropDownList.Items.Add ("Item 3"); }}

No need to repopulate listwhen a postback occurs

Page 27: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Page DirectivesPage Directives

Directive Description

@ Page Defines general attributes and compilation settings for ASPX files

@ Control Defines general attributes and compilation settings for ASCX files

@ Import

@ Assembly

@ Register

@ OutputCache

@ Reference

Imports a namespace

Imports an assembly

Registers user controls and custom controls in Web forms

Provides declarative control over ASP.NET’s page output cache

Adds a reference to an external ASPX or ASCX file

@ Implements Identifies an interface implemented by a Web page

Page 28: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

@ Page Example@ Page Example

<%@ Page Language="C#" Trace="true" %>

Default language

Enable tracing

Page 29: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

Key @ Page AttributesKey @ Page Attributes

Attribute Description

ClassName Specifes name of code-behind class

CompileWith Identifies file containing code-behind class

Culture, UICulture Specifies culture for content localization (e.g., "en-us")

Debug Enables and disables debug symbols (default=false)

ErrorPage Designates error page shown if unhandled execption occurs

Language Specifies language used for inline code (<% … %>)

Trace Enables and disables trace output (default=false)

MasterPageFile Designates the page's master page (if any)

Page 30: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

User ControlsUser Controls

Reusable chunks of HTML and code Reusable chunks of HTML and code packaged in ASCX filespackaged in ASCX files

Registered in ASPX files with @ RegisterRegistered in ASPX files with @ Register

Declared in ASPX files with tag prefixes Declared in ASPX files with tag prefixes and tag names specified in @ Registerand tag names specified in @ Register

Great for replicating UI elements that Great for replicating UI elements that appear multiple times on a given appear multiple times on a given page (or separate pages)page (or separate pages)

Page 31: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

User ControlsUser Controls

Page 32: Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls

© 2003-2004 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.