chapter 6 - asp.net web form

Upload: phannarith

Post on 30-May-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    1/42

    ASP.NET Web Forms

    Main Menu 1 of 42

    ASP.NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    2/42

    ASP.NET Web Forms

    Main Menu 2 of 42

    Objective

    At the end of this chapter, you will be able to

    understand what are ASP.NET Web Forms, how

    Visual Studio.NET creates these.

    You will an understanding of how and why

    Microsoft has separated the user interface resources

    from the business logic.

    You will also get in-depth knowledge of the PageClass, Page, the sequence of events and setting the

    Page Directives.

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    3/42

    ASP.NET Web Forms

    Main Menu 3 of 42

    What are Web Forms?

    ASP.NET Frame work

    Visual Studio ASP.NET Web Applications

    Where Does Visual Studio Fit In?

    Whats in Your Project?

    Web Forms Code Model

    Page Class

    Scope

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    4/42

    ASP.NET Web Forms

    Main Menu 4 of 42

    Location

    Properties of Page class

    Methods

    Events of Page class

    Web Forms page life cycle

    Page_Init Event

    Page_Load Event

    Other Events

    Page_Unload Event

    Scope

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    5/42

    ASP.NET Web Forms

    Main Menu 5 of 42

    ASP.NET Page directives

    Page

    Control

    Import

    Register

    Assembly

    OutputCache Implements

    Reference

    Scope

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    6/42

    ASP.NET Web Forms

    Main Menu 6 of 42

    ASP.NET web forms are nothing but text files,

    saved with an extension of.aspx.

    The web forms can be created in the following three

    ways:

    Notepad

    Front Page

    VB.NET web Forms

    What are Web Forms ?

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    7/42

    ASP.NET Web Forms

    Main Menu 7 of 42

    Web Forms has the following features : -

    Browser-independent applications.

    Event-based programming model.

    Abstract, intuitive, consistent object model.

    State management.

    Scalable server performance.

    ASP.NET Frame Work

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    8/42

    ASP.NET Web Forms

    Main Menu 8 of 42

    Visual Studio ASP.NET Web

    Applications ASP.NET Web applications run on a Web server

    configured with Microsoft IIS.

    You do not need to learn working directly with IISor configuring it. It can all be programmed using

    ASP.NET classes.

    Visual Studio handles all the file management tasks

    such as creating IIS applications when needed and

    providing ways for you to even deploy your Web

    applications to IIS.

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    9/42

    ASP.NET Web Forms

    Main Menu 9 of 42

    The advantage of using Visual Studio is that it

    provides tools that make web Application

    development much faster, easier, universally

    readable and much more robust. These tools include:

    Visual designers

    Code-aware editors

    Integrated compilation and debugging

    Project management

    Where Does Visual Studio Fit In?

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    10/42

    ASP.NET Web Forms

    Main Menu 10 of 42

    The ASP.NET Web Application project template

    automatically adds the essential project references

    and files to use as a starting point for your

    application. When you create a Web project in Visual Studio,

    Web Forms designer is opened.

    The Web Forms designer is the design surface foryour pages.

    Whats in Your Project ?

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    11/42

    ASP.NET Web Forms

    Main Menu 11 of 42

    You can use the design view (which is open in the

    image ) to design the lay out your page and its

    elements, HTML view to view the raw HTML

    generated for your page, and the code-behind viewto look at the code file associated with your page.

    Whats in Your Project ?

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    12/42

    ASP.NET Web Forms

    Main Menu 12 of 42

    When you create a Web project, Visual Studio

    constructs a Web application directory structure on

    the target Web server that you have configured

    (Your IIS in this case), And a project structure on your local computer.

    Whats in Your Project ?

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    13/42

    ASP.NET Web Forms

    Main Menu 13 of 42

    Separation of user interface resources from business

    logic:

    Web Forms divide the Web applications into two pieces:

    User Interface: . It is the visual part, which interacts with the user.

    This part will be dealing with the user.

    Business Logic:

    User interface logic for the Web form consists of code that you

    create to interact with the form in a separate file also known as

    "code-behind" file.

    Web Forms Code Model

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    14/42

    ASP.NET Web Forms

    Main Menu 14 of 42

    When the web form page is compiled, ASP.NET

    generates a new class and then compiles the new

    class.

    This new class is derived from the ASP.NET Pageclass, but is extended with controls, your code, and

    the static HTML text in the. aspx file.

    In ASP.NET entire Web Form is an executableprogram whose output is HTML.

    Page Class

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    15/42

    ASP.NET Web Forms

    Main Menu 15 of 42

    The page goes through a series of processing stagesanalogous with those of other componentsinitialize, process, and dispose.

    The page class performs these stages each time thepage is called; that is, the page is initialized,processed, and disposed every time a call is made tothe server.

    For efficiency the information required to recreatethe page is cached, but this is independent of itslifecycle.

    Page Class

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    16/42

    ASP.NET Web Forms

    Main Menu 16 of 42

    Page class Defines the properties, methods, and

    events common to all pages that are processed on the

    server by the Web Forms page framework.

    Location

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    17/42

    ASP.NET Web Forms

    Main Menu 17 of 42

    Examples of Page Class Properties:

    Write the following code in the page_Load event

    Response.Write(Page.Application)

    Response.Write("
    Is PostBack = " &Page.IsPostBack)

    Properties of Page Class

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    18/42

    ASP.NET Web Forms

    Main Menu 18 of 42

    The output would be:

    Properties of Page Class

    AS b

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    19/42

    ASP.NET Web Forms

    Main Menu 19 of 42

    Write the following code in the Page_Load event to

    see the absolute URL of your web form

    Response.Write(Page.ResolveUrl

    ("WebForm1.aspx"))

    Methods

    ASP NET W b F

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    20/42

    ASP.NET Web Forms

    Main Menu 20 of 42

    Output would be:

    Methods

    ASP NET W b F

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    21/42

    ASP.NET Web Forms

    Main Menu 21 of 42

    With all these properties, methods and events Web

    Forms offer great functionalities to the programmer.

    As every web form ultimately gets compiled in toclass, which is a sub class of Page class, hence this

    new compiled class can access all the methods of its

    parent class.

    Public Instance Events of Page

    class

    ASP NET W b F

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    22/42

    ASP.NET Web Forms

    Main Menu 22 of 42

    Life cycle of any process consists of various stages

    involved in its life.

    The life cycle for a Web Forms page is, similar to

    that of any Web process that runs on the server.

    In Web Forms, most user actions such as clicking a

    button result in a round trip.

    For that reason, the events available in Web Formscontrols are limited.

    Most controls expose a click event.

    Web Forms Page Life Cycle

    ASP NET W b F

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    23/42

    ASP.NET Web Forms

    Main Menu 23 of 42

    Life cycle of a Web Form page can be divided in

    three main events: -

    Page_Init Event

    Page_Load Event

    Other Events

    Page_Unload Event

    Web Forms Page Life Cycle

    ASP NET W b F

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    24/42

    ASP.NET Web Forms

    Main Menu 24 of 42

    This is the first event in page life cycle. This event is

    fired whenever the page is initialized.

    Example:

    Enter the following code in the page_init event

    Response.write(
    In Page init

    event )

    And this would be the first line on your web page.

    Page_Init Event

    ASP NET W b F

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    25/42

    ASP.NET Web Forms

    Main Menu 25 of 42

    This is event is fired when the page is loaded.

    Difference between init and loading event is that the

    controls are fully loaded in the load event only.

    Enter the following code in the Page_Load event

    Response.Write("
    In page load

    Page Loaded at : " &

    DateTime.Now) And this would be the second line on your web page.

    Page_Load Event

    ASP NET W b F

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    26/42

    ASP.NET Web Forms

    Main Menu 26 of 42

    These are the events, which are fired after the

    Page_Load event.

    If the page was called in response to a form event,

    the corresponding event-handling method in thepage is called during this stage.

    Events like button_pressed;

    mouse_clickedetc. can be put here.

    Control Events

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    27/42

    ASP.NET Web Forms

    Main Menu 27 of 42

    This event is called, when the page has performed

    all necessary actions and is ready to be discarded.

    All the coding, necessary before cleaning up, is

    placed here. Few of such tasks could be closing files,closing database connections, or discarding objects.

    It is always better to close the connections, which

    has been opened in your program.

    Page_Unload Event

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    28/42

    ASP.NET Web Forms

    Main Menu 28 of 42

    ASP.NET supports few directives, which are used tospecify optional settings used by the page compilerwhen processing ASP.NET files.

    These directive can be placed anywhere in the pagebut conventionally these are placed at the top of thefile.

    Each directive can contain one or more

    attribute/value pairs specific to that directive.Syntax of using directive as follows: -

    ASP.NET Page Directives

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    29/42

    ASP.NET Web Forms

    Main Menu 29 of 42

    The @ Page directive defines page-specificattributes used by the ASP.NET page parser andcompiler.

    You can only include one @Page directive per.aspx file.

    To define multiple attributes, use a space-separatedlist, with no space around the equals sign, as inTRACE="True".

    @Page

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    30/42

    ASP.NET Web Forms

    Main Menu 30 of 42

    The @ Control directive defines user control-specific attributes used by the ASP.NET pagecompiler.

    This directive can only be used with user controls.

    The @ Control directive supports the same attributes

    as the @ Page directive, except the AspCompat andTrace attributes.

    You can only include one @ Control directive per.ascx file.

    @Control

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    31/42

    ASP.NET Web Forms

    Main Menu 31 of 42

    The @ Import directive explicitly imports anamespace into a page, making all classes andinterfaces of the imported namespace available tothe page.

    The imported namespace can be either part of the.NET Framework class library or a user-definednamespace. Syntax is as follows: -

    The @ Import directive cannot have more than one

    namespaceattribute.

    @Import

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    32/42

    ASP.NET Web Forms

    Main Menu 32 of 42

    The @Register directive associates aliases with

    namespaces and class names for concise notation in

    custom server control syntax.

    When a new namespace is added to a page you haveto specify to the compiler few things about the

    control.

    With this information compiler can recognize the

    control.

    @Register

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    33/42

    ASP.NET Web Forms

    Main Menu 33 of 42

    There are two ways to use this tag: -

    @Register

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    34/42

    ASP.NET Web Forms

    Main Menu 34 of 42

    The @Assembly directive declaratively links anassembly to the current page, making all of theassembly's classes and interfaces available for use onthe page.

    You can also use this directive to register assembliesin the configuration file to link assemblies across theentire application.

    It is not necessary to include the path of an assembly

    in a @ Assembly directive.

    @Assembly

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    35/42

    ASP.NET Web Forms

    Main Menu 35 of 42

    This directive is used to control the caching of a

    page on the server.

    The @OutputCache directive declaratively controls

    the output caching policies of a page.

    @OutputCache

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    36/42

    ASP.NET Web Forms

    Main Menu 36 of 42

    The @implements directive allows you to

    implement a .NET interface in your page.

    When an interface is implemented the page supports

    the defined properties, events and methods of thespecified interface.

    Using the following syntax this directive is used

    @Implements

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    37/42

    ASP.NET Web Forms

    Main Menu 37 of 42

    It declaratively indicates that another user control or

    page source file should be dynamically compiled and

    linked against the current page.

    Attributes

    Page

    Control

    @Reference

    ASP NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    38/42

    ASP.NET Web Forms

    Main Menu 38 of 42

    Key points covered in this chapter are:

    Web forms are used to share information between browser

    and server.

    Web forms are text files only, saved with an extension ofaspx.

    Visual Studio.NET provides an easy to use GUI for

    developing web applications.

    There is no need to create a virtual directory every timewe create a web application in Visual Studio.NET

    Summary

    ASP.NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    39/42

    ASP.NET Web Forms

    Main Menu 39 of 42

    Visual studio provides different views to write different

    types of codes eg. HTML view, Code View, design view

    Visual studio generates a number of files with different

    functionalities in the web project.

    Web forms Code Model provides separation of the html

    code from programming logic.

    Page is represented as a class in .NET framework.

    Page has its own class hierarchy, properties, methods andevents.

    Web forms Page Life Cycle is based on events.

    Summary

    ASP.NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    40/42

    ASP.NET Web Forms

    Main Menu 40 of 42

    Page_Init Event is the first event fired as soon as the page

    is loaded.

    Other Events are events fired by the program depending

    on the page. These events are defined by the developer

    e.g. button click event.

    Page_Unload Event is the last event to write the clean up

    code.

    ASP.NET Page directives are used to declare various

    directives on the page namely Page, Control, Import,

    Register, Assembly and OutputCache. These give our web

    page a default behavior.

    Summary

    ASP.NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    41/42

    ASP.NET Web Forms

    Main Menu 41 of 42

    Fill in the blanks:

    1. ______________event is called, when the page has

    performed all necessary actions and is ready to be

    discarded.2. _____________ is a WYSIWYG editing tool for

    designing and programming our Web Forms.

    3. ASP.NET web forms are nothing but text files, saved

    with an extension of ________.4. Code behind file has a ___________ extension.

    Self Assessment

    ASP.NET Web Forms

  • 8/9/2019 Chapter 6 - ASP.NET Web Form

    42/42

    ASP.NET Web Forms

    State True/False :

    1. Web forms can be created using visual studio.net only.

    2. Web forms are used to share information between

    browser and server.3. Web forms are windows based forms.

    4. Page directives are used to control information with in

    the aspx page.

    Self Assessment