ASP.net Session 08

download ASP.net Session 08

of 33

Transcript of ASP.net Session 08

  • 7/30/2019 ASP.net Session 08

    1/33

    Slide 1 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    In this session, you will learn to:

    Implement server-side state management

    Manage state by using cross-page posting

    Implement caching

    Identify basics of data access in Web applicationsAccess data by using the presentation layer

    Objectives

  • 7/30/2019 ASP.net Session 08

    2/33

    Slide 2 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    ASP.NET provides the profiles feature to implement

    personalization in Web applications.

    This enables developers to store preferences without writing

    much code. These preferences are stored as named

    properties in profiles.The properties that need to be stored in each user profile for

    an application can be configured in the web.config file, as

    shown in the following example:

    Profile Properties

  • 7/30/2019 ASP.net Session 08

    3/33

    Slide 3 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    After configuring the profile properties in the web.config file,you can access them by using the Profile object.

    The value of the profile property can be set, as shown in the

    following code snippet:

    Profile.UserName = TextBox1.Text;The value of profile property can be retrieved, as shown in

    the following code snippet:

    TextBox1.Text = Profile.UserName;

    By default, the profile properties are stored in the aspnetdb

    database.

    Profile Properties (Contd.)

    Let us see how to create the aspnetdb database.

  • 7/30/2019 ASP.net Session 08

    4/33

    Slide 4 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    By default, a Button control on a Web page submits the

    page back to itself.

    Sometimes, you may want to post one page to another.

    In such a case, you can set the PostBackUrl property of

    the Button control to the URL of the target page.This posting of information from one page to another page is

    called cross-page posting.

    Managing State by Using Cross-Page Posting

  • 7/30/2019 ASP.net Session 08

    5/33

    Slide 5 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The Page class exposes a public property named

    PreviousPage.

    If the source and target pages are in the same application,the PreviousPage property contains a reference to the

    source page.You can access the values in the controls on the sourcepage by using the FindControl method.

    You can check whether a page is running as a result of apostback by using the IsPostBack property of the Page

    class.You can check whether a page is running as a result of across-page postback by using the IsCrossPagePostBack

    property of the Page class.

    Managing State by Using Cross-Page Posting (Contd.)

  • 7/30/2019 ASP.net Session 08

    6/33

    Slide 6 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Problem Statement:

    The management at MusicMania wants to allow the users to

    place orders for music CDs online. For this, a user needs to

    register on the website before placing the order. The

    registration process requires the user to fill a form, which

    contains user-specific information. After filling the form, theuser needs to click the Submit button. After the registration is

    successful, a new Web page should appear that displays the

    message "Welcome , you have successfully

    registered."

    You have been asked by the team leader to implement this

    functionality on the MusicMania website.

    Activity 5.3: Managing State by Using Cross-Page Posting

  • 7/30/2019 ASP.net Session 08

    7/33Slide 7 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Solution:

    To display the user name on another Web page by using

    cross-page posting, you need to perform the following tasks:

    1. Add a new Web page.

    2. Design the Web page.

    3. Modify the Home page.

    4. Add a new Web page.

    5. Design the Web page.

    6. Verify the application.

    Activity 5.3: Managing State by Using Cross-Page Posting (Contd.)

  • 7/30/2019 ASP.net Session 08

    8/33Slide 8 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Speed is a critical factor that judges the success or failure of

    any website.

    When multiple users access a website simultaneously, slow

    access to Web pages is a common problem that arises.

    The problem of slow access can be solved by using atechnique called caching.

    Caching:

    Improves the performance of any Web application by

    temporarily storing frequently used Web objects, such as

    HTML pages, on local hard disks for later retrieval.Enables you to improve the performance of Web applications.

    Implementing Caching

  • 7/30/2019 ASP.net Session 08

    9/33Slide 9 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The major benefits of using Web caching are:

    Reduced access time

    Less bandwidth required

    Less load on server

    ASP.NET supports three types of caching:Output Caching

    Fragment Caching

    Data Caching

    Implementing Caching (Contd.)

  • 7/30/2019 ASP.net Session 08

    10/33Slide 10 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Output caching:

    Improves the performance of an ASP.NET application by

    caching the rendered markup of an ASP.NET Web page.

    Is useful when the content of the Web page changes rarely.

    To implement output caching, you need to insert the@ OutputCache directive, as shown in the following

    example:

    Output Caching

  • 7/30/2019 ASP.net Session 08

    11/33Slide 11 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    In fragment caching, the data from some specific sections of

    the page can be cached.

    It is also referred to as partial page caching.

    To implement fragment caching, the portion of the page that

    is to be cached is encapsulated in a user control, and thenthe @ OutputCache directive is added to the control, as

    shown in the following example:

    Fragment Caching

  • 7/30/2019 ASP.net Session 08

    12/33Slide 12 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Data caching is used to store frequently used data in thebuilt-in collection object called Cache.

    It is a property of the Page class, and it returns an instance

    of the System.Web.Caching.Cache class.

    The Cache object:Works like the Application object.

    Is globally available to all requests from all clients in the

    application.

    Differs from the Application object in the following ways:

    TheCache

    object does not need to be explicitly locked or

    unlocked.

    Items in the Cache object are removed automatically.

    Items in the Cache object can be linked to a file, a database

    table, or another type of resource.

    Data Caching

  • 7/30/2019 ASP.net Session 08

    13/33Slide 13 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    You can store a variable by the name, uname, in the Cache

    object, and assign it a value, as shown in the following code

    snippet:

    Cache ["uname"] = "Robert";

    You can retrieve the value from the Cache object, as shownin the following code snippet:TextBox1.Text = Cache["uname"];

    Data Caching (Contd.)

  • 7/30/2019 ASP.net Session 08

    14/33Slide 14 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Most Web applications are designed by using the multiple

    tier model known as the N-tier model.

    The following figure shows a basic N-tier model.

    Identifying the Basics of Data Access in Web Applications

    DATA ACCESS LAYER

    BUSINESS RULES LAYER

    PRESENTATION LAYER

  • 7/30/2019 ASP.net Session 08

    15/33Slide 15 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The presentation layer, in a Web application, is the interface

    that appears when a user opens a Web page in the browser.

    If you see the source code of the page, you would only see

    the code such as HTML, Javascript, and Cascading Style

    Sheets.

    You would not be able to see the database queries, loops,

    calls to classes, or any behind-the-scenes processing.

    The presentation layer does not contain any data access

    code or program logic.

    In case of small Web applications, you may find the databaseaccess layer merged with the presentation layer.

    Presentation Layer

  • 7/30/2019 ASP.net Session 08

    16/33Slide 16 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The business rules layer:

    Is also known as the business logic tier or the application tier.

    Contains all the classes and the source code of the Web

    application.

    Allows you to separate the application logic from the user

    interface of a Web page.

    Enables the programmer to easily search for a specific code

    because the code is not cluttered with HTML or Javascript.

    Does not contain HTML or JavaScript code.

    Business Rules Layer

  • 7/30/2019 ASP.net Session 08

    17/33Slide 17 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    You can implement the data access logic in the presentation

    layer; however this is not recommended because:

    It clutters the data access code with the HTML and Javascript

    code.

    It tightly couples the data access logic with the presentation

    layer. This prevents the data access logic to be reused in other

    Web applications.

    All the preceding limitations can be overcome by separating

    the data access logic from the presentation layer.

    It can be done by creating a new layer and implementing the

    data access logic in it.

    This separate layer is known as DAL, which is typically

    implemented as a separate class library.

    Database Access Layer

  • 7/30/2019 ASP.net Session 08

    18/33Slide 18 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Most of the modern day Web applications need to manipulate

    data in databases. This can be done by implementing data

    access logic in applications.

    If the data access logic is not to be reused in any other

    application, then the logic can be implemented in the

    presentation layer (ASP.NET pages). This can be done by

    using:

    Data source controls

    ADO.NET

    Accessing Data by Using the Presentation Layer

  • 7/30/2019 ASP.net Session 08

    19/33Slide 19 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Data source controls:

    Allow you to work with different types of data sources such as a

    database, an XML file, or a middle-tier business object.

    Act as a bridge between the application and the data sources.

    Can connect to the data sources and retrieve data without

    requiring you to write any code.

    Do not provide the interface to display data. To display the

    retrieved data, it is made available to the data controls or data-

    aware controls.

    Data Source Controls

  • 7/30/2019 ASP.net Session 08

    20/33Slide 20 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Data source controls perform the following tasks:

    Retrieve data from a data source and supply it to the data

    controls or data aware controls.

    Update the data source when edits take place.

    Data source controls in ASP.NET:

    SqlDataSource

    AccessDataSource

    ObjectDataSource

    XmlDataSource

    SiteMapDataSourceLinqDataSource

    Data Source Controls (Contd.)

  • 7/30/2019 ASP.net Session 08

    21/33Slide 21 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The SqlDataSource control:

    Is used to access data from an SQL relational database.

    Can be used to access any database product for which there is

    a managed ADO.NET provider.

    Is used in conjunction with data controls.

    The following table lists some properties of the

    SqlDataSource control.

    Data Source Controls (Contd.)

    Proper ty Descr ipt ion

    ProviderName Gets or sets the name of the . provider that is used to

    connect to an underlying data source.ConnectionString Gets or sets the connection string that is used to

    connect to an underlying data source.SelectCommand Gets or sets the SQL string that is used to retrieve

    data from the underlying data source.DeleteCommand Gets or sets the SQL string that is used to delete data

    from the underlying data source.InsertCommand Gets or sets the SQL string that is used to insert data

    in the underlying data source.UpdateCommand Gets or sets the SQL string that is used to update data

    in the underlying data source.

  • 7/30/2019 ASP.net Session 08

    22/33Slide 22 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The AccessDataSource control:

    Works with Microsoft Access databases.

    Uses SQL queries to perform data retrieval.

    Cannot be used to access Access databases that are protected

    by a user name or password.

    The following table lists some properties of the

    AccessDataSource control.

    Data Source Controls (Contd.)

    Proper ty Descr ipt ion

    ProviderName Gets or sets the name of the . provider that is used to

    connect to a Microsoft Access.DataFile Gets or sets the location of the Microsoft Access file.

    SelectCommand Gets or sets the SQL string that is used to retrieve

    data from the underlying data source.DeleteCommand Gets or sets the SQL string that is used to delete

    data from the underlying data source.InsertCommand Gets or sets the SQL string that is used to insert data

    in the underlying data source.UpdateCommand Gets or sets the SQL string that is used to update

    data in the underlying data source.

  • 7/30/2019 ASP.net Session 08

    23/33

    Slide 23 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The ObjectDataSource control:

    Represents a middle-tier object that enables developers to

    access data from business objects.

    The following table lists some properties of the

    ObjectDataSource control.

    Data Source Controls (Contd.)

    Property Descript ion

    TypeName Identifies the name of the class that the ObjectDataSource works

    with.SelectMethod Gets or sets the method or function that is invoked to retrieve

    data.SelectParameters Gets the parameters collection that contains the parameters used

    by the SelectMethodproperty.

    InsertMethod Gets or sets the method or function that is invoked to insert data.

    InsertParameters Gets the parameters collection that contains the parameters usedby the InsertMethodproperty.

    UpdateMethod Gets or sets the method or function that is invoked to update data.

    UpdateParameters Gets the parameters collection that contains the parameters usedby the UpdateMethodproperty.

    DeleteMethod Gets or sets the method or function that is invoked to delete data.

    DeleteParameters Gets the parameters collection that contains the parameters usedby the DeleteMethodproperty.

  • 7/30/2019 ASP.net Session 08

    24/33

    Slide 24 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The XmlDataSource control:

    Presents XML data to data-bound controls.

    Can be used by data-bound controls to display hierarchical as

    well as tabular data.

    Loads data from an XML file that is specified by the DataFile

    property.

    Can also store XML data in string form by using the Data

    property.

    The following table lists some properties of the

    XmlDataSource control.

    Data Source Controls (Contd.)

    Proper ty Descr ipt ion

    DataFile Specifies the filename of the XML file that is to be

    bound with the XmlDataSource control.Data Gets or sets the block of xml data that is to be bound

    with the XmlDataSource control in string form.

  • 7/30/2019 ASP.net Session 08

    25/33

    Slide 25 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    The SiteMapDataSource control:

    Provides navigation data to the navigational controls.

    Enables Web server controls that are not specifically site

    navigation controls, such as the TreeView and Menu controls, to

    bind to site map data.

    Can be bound to the navigational controls by using the

    DataSourceID property of the navigational controls.

    The following table lists some properties of the

    SiteMapDataSource control.

    Data Source Controls (Contd.)

    Proper ty Descr ipt ion

    SiteMapProvider Gets or sets the name of the site map provider.

    StartingUrlNode Used to set the URL in the sitemap that will be

    considered as the root node.ShowStartingNode Indicates whether to show starting node or not. It can

    have two values, True orFalse.

  • 7/30/2019 ASP.net Session 08

    26/33

    Slide 26 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Data-bound Web server controls:

    Can be bound to a data source control to display and modify

    data in a Web application.

    Are composite controls that combine other Web controls, such

    as Label and TextBox controls into a single unit.

    Enable you to customize the layout of the control using

    templates.

    Displaying Data in Data-Bound Web Server Controls

  • 7/30/2019 ASP.net Session 08

    27/33

    Slide 27 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    ASP.NET provides following data-bound Web server controls:

    GridView control

    DetailsView control

    FormView control

    ListView controlDataList control

    Repeater control

    DataPager control

    Let us see how to retrieve data from a data source by usingthe SqlDataSource control

    Displaying Data in Data-Bound Web Server Controls (Contd.)

  • 7/30/2019 ASP.net Session 08

    28/33

    Slide 28 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Binding controls to data by using data binding syntax:

    Data binding is a technique of linking data and user interface

    objects.

    Using data binding, you can bind data in a data source to a user

    interface object.

    On the basis of the number of bound values that can be

    displayed through a user interface object, binding can be

    classified into:

    Single-value data binding

    Repeated-value data binding

    Binding Controls to Data by Using Data Binding Syntax

  • 7/30/2019 ASP.net Session 08

    29/33

    Slide 29 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Single-value data binding:

    Single-value data binding involves binding the controls that can

    display one data value at a time to a data source.

    Single-value controls do not support any data bindingproperties, such as the DataSource property or the

    DataMember property.

    Binding Controls to Data by Using Data Binding Syntax (Contd.)

  • 7/30/2019 ASP.net Session 08

    30/33

    Slide 30 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Consider a case where you need to bind a Label control to a

    public variable that displays the current date and time. For this,

    you can write the following code in the .aspx.cs file:

    public partial class _Default : System.Web.UI.Page

    {

    public string dt = DateTime.Now.ToString();

    protected void Page_Load(object sender, EventArgs e)

    {

    this.DataBind();/*binds the data source to the

    invoked server control and all its child

    controls*/}

    }

    Binding Controls to Data by Using Data Binding Syntax (Contd.)

  • 7/30/2019 ASP.net Session 08

    31/33

    Slide 31 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    Repeated-value data binding:

    Repeated-value data binding involves binding server controls to

    structures, such as ArrayList and DataView objects, whichimplement the IEnumerable interface.

    The following example shows how you can bind an ArrayList to

    a ListBox control:

    protected void Page_Load(object sender, EventArgs e)

    {

    ArrayList Month = new ArrayList();

    Month.Add("Jan");

    Month.Add("Feb");Month.Add("Mar");

    Month.Add("Apr");

    ListBox1.DataSource = Month;

    ListBox1.DataBind();

    }

    Binding Controls to Data by Using Data Binding Syntax (Contd.)

  • 7/30/2019 ASP.net Session 08

    32/33

    Slide 32 of 33Ver. 1.0

    Developing Web Applications Using ASP.NET

    In this session, you learned that:

    The profiles feature is used to implement personalization in

    Web applications.

    Caching improves the performance of a Web application by

    temporarily storing frequently used Web objects on local hard

    disks for later retrieval.

    ASP.NET supports three types of caching:

    Output Caching

    Fragment Caching

    Data Caching

    The basic N-tier model consists of the following layers:Presentation Layer

    Business Rules Layer

    Database Access Layer

    Summary

  • 7/30/2019 ASP.net Session 08

    33/33

    Developing Web Applications Using ASP.NET

    You can implement data access logic in the presentation layer

    (ASP.NET pages) by using:

    Data source controls

    ADO.NET

    Summary (Contd.)