Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls."— Presentation transcript:

1 Web Forms

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

3 HTML Form (Calc.html) +

4 ASP Form (Calc.asp) "/> + " /> <% If Request ("op1") <> "" And Request ("op2") <> "" Then a = CInt (Request ("op1")) b = CInt (Request ("op2")) Response.Write (CStr (a + b)) End If %>

5 Web Form (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 (); }

6 ASP.NET Web Forms

7 Web Controls Approximately 80 in all ~30 carried forward from ASP.NET 1.x ~50 new controls in ASP.NET 2.0 Rich programming model that permits difficult UIs (such as menus and tree views) to be encapsulated in easy-to-use classes Open architecture that enables developers to write custom controls of their own

8 “Simple” Controls NameDescription LabelRenders programmable HTML text TextBoxRenders programmable HTML text boxes HyperLinkRenders programmable HTML hyperlinks CheckBoxRenders programmable HTML check boxes RadioButtonRenders programmable HTML radio buttons ImageRenders programmable static images

9 “Simple” Controls, Cont. NameDescription TableRenders programmable HTML tables PanelProvides container for grouping other controls PlaceHolderServes as placeholder for adding controls at run-time HiddenFieldRenders hidden fields ( ) ImageMapRenders programmable HTML image maps FileUploadProvides UI for uploading files

10 Button Controls NameDescription ButtonRenders push button that posts back and fires Click event when clicked LinkButtonRenders hyperlink that posts back and fires Click event when clicked ImageButtonRenders image that posts back and fires Click event when clicked

11 Using the TextBox, Label, and Button Controls

12 List Controls NameDescription ListBoxRenders progammable HTML list boxes DropDownListRenders programmable HTML drop-down lists RadioButtonListRenders programmable groups of HTML radio buttons CheckBoxListRenders programmable groups of HTML check boxes BulletedListRenders programmable bulleted lists

13 Using DropDownList

14 Validation Controls NameDescription CompareValidatorCompares 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

15 Input Validation

16 Code Separation Web forms support two* coding models: Code-inline - Markup and code in ASPX Code-behind - Markup in ASPX, code in CS/VB Visual Studio 2005 supports both models No difference in performance * Not including code-behind 1.0, which is deprecated in ASP.NET 2.0

17 Code-Inline + void OnAdd (Object sender, EventArgs e) { int a = Convert.ToInt32 (op1.Text); int b = Convert.ToInt32 (op2.Text); Sum.Text = (a + b).ToString (); } Calc.aspx

18 Code-Behind + Calc.aspx Corresponding source code file Class name in source code file

19 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)

20 Dynamic Page Compilation Parser DLL HTTP RequestHTTP Response Compiler Once compiled, assembly reused in subsequent requests public partial class Calc_aspx : System.Web.UI.Page,... {... } public partial class Calc_aspx : System.Web.UI.Page,... {... } ASPX public partial class Calc_aspx { // Your code } public partial class Calc_aspx { // Your code } Parser generates partial class derived from Page Calc.aspx.cs a0b1c3d4.cs Source code files passed to compiler Compiler produces assembly containing Page-derived class

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

22 System.Web.UI.Page Base class for pages; container for controls Your code executes in context of a Page- derived class, providing intrinsic access to Page members (e.g., Request & Response) Parser adds useful properties of its own to Page- derived classes (e.g., Profile & Resources) Page objects fire events that you can handle by implementing specially named methods Page lifetime is one request

23 Key Page Properties NameDescription CacheProvides access to the application cache ControlsProvides access to the page’s controls IsPostBackTrue if page is executing due to a postback RequestEncapsulates the current HTTP request ResponseEncapsulates the outgoing HTTP response SessionProvides access to session state Type Cache ControlCollection Boolean HttpRequest HttpResponse HttpSessionState UserProvides information about requestor’s identityIPrincipal MasterProvides programmatic access to master pageMasterPage

24 Using Page Properties if (!String.IsNullOrEmpty (Request["ItemID"])) { Response.Write (Request["ItemID"]); } Page.Request property refers to HttpRequest object representing the request Page.Response property refers to HttpResponse object representing the response

25 The Page_Load Method If present, called automatically in every request when Page object fires Load event Called early in page's lifetime Controls have been created and initialized No rendering has been done (yet) Perfect place to initialize controls programmatically and to bind controls to data sources

26 Page_Load Example... 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 list when a postback occurs

27 Page Directives DirectiveDescription @ PageDefines general attributes and compilation settings for ASPX files @ ControlDefines 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 @ ImplementsIdentifies an interface implemented by a Web page

28 @ Page Example Default language Enable tracing

29 Key @ Page Attributes AttributeDescription ClassNameSpecifes name of code-behind class CompileWithIdentifies file containing code-behind class Culture, UICultureSpecifies culture for content localization (e.g., "en-us") DebugEnables and disables debug symbols (default=false) ErrorPageDesignates error page shown if unhandled execption occurs LanguageSpecifies language used for inline code ( ) TraceEnables and disables trace output (default=false) MasterPageFileDesignates the page's master page (if any)

30 User Controls Reusable chunks of HTML and code packaged in ASCX files Registered in ASPX files with @ Register Declared in ASPX files with tag prefixes and tag names specified in @ Register Great for replicating UI elements that appear multiple times on a given page (or separate pages)

31 User Controls

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


Download ppt "Web Forms. Agenda Web forms Web controls Code separation Dynamic compilation System.Web.UI.Page User controls."

Similar presentations


Ads by Google