Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
276 views

Session 08

This document discusses various techniques for developing web applications using ASP.NET, including managing state through profile properties, cross-page posting, caching, and data access. Profile properties allow storing user preferences in named properties. Cross-page posting enables posting data from one page to another. Caching improves performance by temporarily storing frequently used web objects. Data can be accessed using data source controls or ADO.NET from the presentation layer.

Uploaded by

Prerana Tokas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
276 views

Session 08

This document discusses various techniques for developing web applications using ASP.NET, including managing state through profile properties, cross-page posting, caching, and data access. Profile properties allow storing user preferences in named properties. Cross-page posting enables posting data from one page to another. Caching improves performance by temporarily storing frequently used web objects. Data can be accessed using data source controls or ADO.NET from the presentation layer.

Uploaded by

Prerana Tokas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 33

Developing Web Applications Using ASP.

NET
Objectives

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 applications Access data by using the presentation layer

Ver. 1.0

Slide 1 of 33

Developing Web Applications Using ASP.NET


Profile Properties

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> <add name="UserName" /> <add name="BirthDate" type="System.DateTime" /> </properties> </profile>

Ver. 1.0

Slide 2 of 33

Developing Web Applications Using ASP.NET


Profile Properties (Contd.)

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.


Let us see how to create the aspnetdb database.

Ver. 1.0

Slide 3 of 33

Developing Web Applications Using ASP.NET


Managing State by Using Cross-Page Posting

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.

Ver. 1.0

Slide 4 of 33

Developing Web Applications Using ASP.NET


Managing State by Using Cross-Page Posting (Contd.) 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 source page by using the FindControl method. You can check whether a page is running as a result of a postback by using the IsPostBack property of the Page class. You can check whether a page is running as a result of a cross-page postback by using the IsCrossPagePostBack property of the Page class.

Ver. 1.0

Slide 5 of 33

Developing Web Applications Using ASP.NET


Activity 5.3: Managing State by Using Cross-Page Posting

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, the user needs to click the Submit button. After the registration is successful, a new Web page should appear that displays the message "Welcome <user name>, you have successfully registered." You have been asked by the team leader to implement this functionality on the MusicMania website.

Ver. 1.0

Slide 6 of 33

Developing Web Applications Using ASP.NET


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

Solution:
To display the user name on another Web page by using cross-page posting, you need to perform the following tasks:
1. 2. 3. 4. 5. 6. Add a new Web page. Design the Web page. Modify the Home page. Add a new Web page. Design the Web page. Verify the application.

Ver. 1.0

Slide 7 of 33

Developing Web Applications Using ASP.NET


Implementing Caching

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 a technique 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.

Ver. 1.0

Slide 8 of 33

Developing Web Applications Using ASP.NET


Implementing Caching (Contd.)

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

Ver. 1.0

Slide 9 of 33

Developing Web Applications Using ASP.NET


Output Caching

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:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ OutputCache Duration="20" VaryByParam="None" %>

Ver. 1.0

Slide 10 of 33

Developing Web Applications Using ASP.NET


Fragment Caching

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 then the @ OutputCache directive is added to the control, as shown in the following example:
<%@ Control Language="C#"%> <%@ OutputCache Duration="120" VaryByParam="None" %>

Ver. 1.0

Slide 11 of 33

Developing Web Applications Using ASP.NET


Data Caching

Data caching is used to store frequently used data in the built-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:
The Cache 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.

Ver. 1.0

Slide 12 of 33

Developing Web Applications Using ASP.NET


Data Caching (Contd.) 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 shown in the following code snippet:
TextBox1.Text = Cache["uname"];

Ver. 1.0

Slide 13 of 33

Developing Web Applications Using ASP.NET


Identifying the Basics of Data Access in Web Applications

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.

DATA ACCESS LAYER

BUSINESS RULES LAYER

PRESENTATION LAYER

Ver. 1.0

Slide 14 of 33

Developing Web Applications Using ASP.NET


Presentation Layer

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 database access layer merged with the presentation layer.

Ver. 1.0

Slide 15 of 33

Developing Web Applications Using ASP.NET


Business Rules Layer

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.

Ver. 1.0

Slide 16 of 33

Developing Web Applications Using ASP.NET


Database Access Layer

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.

Ver. 1.0

Slide 17 of 33

Developing Web Applications Using ASP.NET


Accessing Data by Using the Presentation Layer

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

Ver. 1.0

Slide 18 of 33

Developing Web Applications Using ASP.NET


Data Source Controls

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 dataaware controls.

Ver. 1.0

Slide 19 of 33

Developing Web Applications Using ASP.NET


Data Source Controls (Contd.)

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 SiteMapDataSource LinqDataSource

Ver. 1.0

Slide 20 of 33

Developing Web Applications Using ASP.NET


Data Source Controls (Contd.)

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.


Property ProviderName ConnectionString SelectCommand DeleteCommand InsertCommand UpdateCommand Description Gets or sets the name of the . provider that is used to connect to an underlying data source. Gets or sets the connection string that is used to connect to an underlying data source. Gets or sets the SQL string that is used to retrieve data from the underlying data source. Gets or sets the SQL string that is used to delete data from the underlying data source. Gets or sets the SQL string that is used to insert data in the underlying data source. Gets or sets the SQL string that is used to update data in the underlying data source. Slide 21 of 33

Ver. 1.0

Developing Web Applications Using ASP.NET


Data Source Controls (Contd.)

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.


Property ProviderName DataFile SelectCommand DeleteCommand InsertCommand Description Gets or sets the name of the . provider that is used to connect to a Microsoft Access. Gets or sets the location of the Microsoft Access file. Gets or sets the SQL string that is used to retrieve data from the underlying data source. Gets or sets the SQL string that is used to delete data from the underlying data source. Gets or sets the SQL string that is used to insert data in the underlying data source. Gets or sets the SQL string that is used to update data in the underlying data source. Slide 22 of 33

UpdateCommand

Ver. 1.0

Developing Web Applications Using ASP.NET


Data Source Controls (Contd.)

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.
Property TypeName SelectMethod SelectParameters InsertMethod InsertParameters UpdateMethod UpdateParameters DeleteMethod DeleteParameters Description Identifies the name of the class that the ObjectDataSource works with. Gets or sets the method or function that is invoked to retrieve data. Gets the parameters collection that contains the parameters used by the SelectMethod property. Gets or sets the method or function that is invoked to insert data. Gets the parameters collection that contains the parameters used by the InsertMethod property. Gets or sets the method or function that is invoked to update data. Gets the parameters collection that contains the parameters used by the UpdateMethod property. Gets or sets the method or function that is invoked to delete data. Gets the parameters collection that contains the parameters used by the DeleteMethod property. Slide 23 of 33

Ver. 1.0

Developing Web Applications Using ASP.NET


Data Source Controls (Contd.)

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.


Property
DataFile Data

Description
Specifies the filename of the XML file that is to be bound with the XmlDataSource control. Gets or sets the block of xml data that is to be bound with the XmlDataSource control in string form.

Ver. 1.0

Slide 24 of 33

Developing Web Applications Using ASP.NET


Data Source Controls (Contd.)

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.


Property SiteMapProvider StartingUrlNode Description Gets or sets the name of the site map provider. Used to set the URL in the sitemap that will be considered as the root node. Indicates whether to show starting node or not. It can have two values, True or False.

ShowStartingNode

Ver. 1.0

Slide 25 of 33

Developing Web Applications Using ASP.NET


Displaying Data in Data-Bound Web Server Controls

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.

Ver. 1.0

Slide 26 of 33

Developing Web Applications Using ASP.NET


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

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


GridView control DetailsView control FormView control ListView control DataList control Repeater control DataPager control Let us see how to retrieve data from a data source by using the SqlDataSource control

Ver. 1.0

Slide 27 of 33

Developing Web Applications Using ASP.NET


Binding Controls to Data by Using Data Binding Syntax

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

Ver. 1.0

Slide 28 of 33

Developing Web Applications Using ASP.NET


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

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 binding properties, such as the DataSource property or the DataMember property.

Ver. 1.0

Slide 29 of 33

Developing Web Applications Using ASP.NET


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

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*/ } }

Ver. 1.0

Slide 30 of 33

Developing Web Applications Using ASP.NET


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

Repeated-value data binding:


Repeated-value data binding involves binding server controls to structures, such as ArrayList and DataView objects, which implement 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(); }
Ver. 1.0

Slide 31 of 33

Developing Web Applications Using ASP.NET


Summary

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

Ver. 1.0

Slide 32 of 33

Developing Web Applications Using ASP.NET


Summary (Contd.)

You can implement data access logic in the presentation layer (ASP.NET pages) by using:
Data source controls ADO.NET

Ver. 1.0

Slide 33 of 33

You might also like