Session 08
Session 08
NET
Objectives
Ver. 1.0
Slide 1 of 33
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
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;
Ver. 1.0
Slide 3 of 33
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
Ver. 1.0
Slide 5 of 33
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
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
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
Ver. 1.0
Slide 9 of 33
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
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
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
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
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.
PRESENTATION LAYER
Ver. 1.0
Slide 14 of 33
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
Ver. 1.0
Slide 16 of 33
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
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
Ver. 1.0
Slide 19 of 33
Ver. 1.0
Slide 20 of 33
Ver. 1.0
UpdateCommand
Ver. 1.0
Ver. 1.0
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
ShowStartingNode
Ver. 1.0
Slide 25 of 33
Ver. 1.0
Slide 26 of 33
Ver. 1.0
Slide 27 of 33
Ver. 1.0
Slide 28 of 33
Ver. 1.0
Slide 29 of 33
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
Slide 31 of 33
Ver. 1.0
Slide 32 of 33
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