Slide 1 of 33
Ver. 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 applications Access data by using the presentation layer
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>
<add name="UserName" />
<add name="BirthDate" type="System.DateTime" /> </properties>
</profile>
Slide 3 of 33
Ver. 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.)
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.
Slide 5 of 33
Ver. 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 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.
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, 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.
Slide 7 of 33
Ver. 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.
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 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.
Slide 9 of 33
Ver. 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
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:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ OutputCache Duration="20" VaryByParam="None" %>
Slide 11 of 33
Ver. 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 then
the @ OutputCache directive is added to the control, as
shown in the following example:
<%@ Control Language="C#"%>
<%@ OutputCache Duration="120" VaryByParam="None" %>
Developing Web Applications Using ASP.NET
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.
Slide 13 of 33
Ver. 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 shown
in the following code snippet:
TextBox1.Text = Cache["uname"];
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
Slide 15 of 33
Ver. 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 database
access layer merged with the presentation layer.
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.
Slide 17 of 33
Ver. 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.
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
Slide 19 of 33
Ver. 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.
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 SiteMapDataSource LinqDataSource
Slide 21 of 33
Ver. 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.)
Property Description
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.
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.)
Property Description
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.
Slide 23 of 33
Ver. 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 Description
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 SelectMethod property.
InsertMethod Gets or sets the method or function that is invoked to insert data. InsertParameters Gets the parameters collection that contains the parameters used
by the InsertMethod property.
UpdateMethod Gets or sets the method or function that is invoked to update data. UpdateParameters Gets the parameters collection that contains the parameters used
by the UpdateMethod property.
DeleteMethod Gets or sets the method or function that is invoked to delete data. DeleteParameters Gets the parameters collection that contains the parameters used
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.)
Property Description
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.
Slide 25 of 33
Ver. 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.)
Property Description
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 or False.
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.
Slide 27 of 33
Ver. 1.0
Developing Web Applications Using ASP.NET
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
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
Slide 29 of 33
Ver. 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 binding properties, such as the DataSource property or the DataMember property.
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*/ }
}
Slide 31 of 33
Ver. 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, 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(); }
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
Slide 33 of 33
Ver. 1.0
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